Advertisement
glcanvas

Untitled

May 24th, 2017
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn constant[value]
  2.   (fn [v] value))
  3.  
  4. (defn variable[value]
  5.   (fn [arguments] (arguments value)))
  6.  
  7. (defn binary [action]
  8.   (fn [left right]
  9.     (fn [variable]
  10.       (action (left variable) (right variable)))))
  11.  
  12. (defn unary [action]
  13.   (fn [left]
  14.     (fn [variable]
  15.       (action (left variable)))))
  16.  
  17. (def add (binary +))
  18.  
  19. (def subtract (binary -))
  20.  
  21. (def multiply (binary *))
  22.  
  23. (def divide (binary (fn[^double a ^double b] (/ a b))))
  24.  
  25. (def negate (unary -))
  26.  
  27. (def sin(unary (fn[^double a] (Math/sin a))))
  28.  
  29. (def cos (unary (fn[^double a] (Math/cos a))))
  30.  
  31. (def BinaryActions {'+ add '- subtract '* multiply '/ divide})
  32.  
  33. (def UnaryAction {'negate negate 'sin sin 'cos cos})
  34.  
  35. (defn parser[string]
  36.   (cond (number? string) (constant string)
  37.         (symbol? string) (variable (str string))
  38.         (seq? string)
  39.                          (if (UnaryAction (first string))
  40.                              ((UnaryAction (first string)) (parser (second string)))
  41.  
  42.                              ((BinaryActions (first string)) (parser (second string)) (parser (nth string 2))))))
  43.  
  44. (defn parseFunction[string]
  45.   (parser (read-string string)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement