Advertisement
Guest User

Untitled

a guest
May 4th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn abstractFunction [op name]
  2.   (fn [& operands]
  3.     (assoc {}
  4.       :evaluate (fn [values] (apply op (map #((:evaluate %) values) operands)))
  5.       :toString (reduce str ["( " name (apply str #(:toString %) operands) " )"]))))
  6.  
  7.  
  8. (defn evaluate [this vars]
  9.   ((:evaluate this) vars))
  10.  
  11. (defn toString [this]
  12.   (:toString this))
  13.  
  14. (defn Constant [val]
  15.   (assoc {}
  16.     :evaluate (constantly val)
  17.     :toString (str val)))
  18.  
  19. (defn Variable [val]
  20.   (assoc {}
  21.     :evaluate (fn [variables] (variables val))
  22.     :toString (str val)))
  23.  
  24. (def Add (abstractFunction + "+"))
  25. (def Subtract (abstractFunction - "-"))
  26. (def Divide (abstractFunction / "/"))
  27. (def Multiply (abstractFunction * "*"))
  28. (def Negate (abstractFunction - "-"))
  29.  
  30. (print (toString (Constant 10.0)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement