Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn abstractFunction [op name]
- (fn [& operands]
- (assoc {}
- :evaluate (fn [values] (apply op (map #((:evaluate %) values) operands)))
- :toString (reduce str ["( " name (apply str #(:toString %) operands) " )"]))))
- (defn evaluate [this vars]
- ((:evaluate this) vars))
- (defn toString [this]
- (:toString this))
- (defn Constant [val]
- (assoc {}
- :evaluate (constantly val)
- :toString (str val)))
- (defn Variable [val]
- (assoc {}
- :evaluate (fn [variables] (variables val))
- :toString (str val)))
- (def Add (abstractFunction + "+"))
- (def Subtract (abstractFunction - "-"))
- (def Divide (abstractFunction / "/"))
- (def Multiply (abstractFunction * "*"))
- (def Negate (abstractFunction - "-"))
- (print (toString (Constant 10.0)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement