2011-10-03 4 views
1

개체의 상태를 변경하고 개체에 새 속성 (특성/상태)을 추가하고 싶습니다 ... 추가하려는 새 속성의 이름은 "세금 ". 나는 아래의 코드를 tryed하지만, PLZ 도와주세요 ... 해결 didnt가 :변경 struct-object 상태

(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