2011-10-03 82 views
1

我想改变对象的状态andre和blastoise,向对象添加一个新的属性(属性/状态)...我想添加的这个新属性的名称是“tax ”。我tryed下面的代码,但没有工作了...帮助我PLZ:更改结构对象状态

(def andre {:owner "Andre" :type "car" :cur-speed 101 :license-plate "ABC"}) 
(def blastoise {:owner "Blastoise" :type "truck" :cur-speed 120 :license-plate "XYZ"}) 
(def car-tax[andre blastoise]) 
(defn calculate-car-tax [v] 
    (for [element v] 
     (if (> (element :cur-speed) 100) (dosync (alter element tax :20))) 
    ) 
) 

(计算-车板含税车板含税)

回答

3

尝试

(assoc andre :tax 20) 

从文档:

user=> (doc assoc) 
------------------------- 
clojure.core/assoc 
([map key val] [map key val & kvs]) 
    assoc[iate]. When applied to a map, returns a new map of the 
    same (hashed/sorted) type, that contains the mapping of key(s) to 
    val(s). When applied to a vector, returns a new vector that 
    contains val at index. Note - index must be <= (count vector). 
nil 
user=> 

编辑为包含功能

(defn calculate-car-tax [v]                                                              
    (for [element v]                                                               
    (if (> (element :cur-speed) 100) 
     (dosync (assoc element :tax 20)))))                                                                                                                   
+0

非常感谢! – andre

+0

@andre没有问题:) – edwardsmatt