Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn constant[value]
- (fn [v] value))
- (defn variable[value]
- (fn [arguments] (arguments value)))
- (defn binary [action]
- (fn [left right]
- (fn [variable]
- (action (left variable) (right variable)))))
- (defn unary [action]
- (fn [left]
- (fn [variable]
- (action (left variable)))))
- (def add (binary +))
- (def subtract (binary -))
- (def multiply (binary *))
- (def divide (binary (fn[^double a ^double b] (/ a b))))
- (def negate (unary -))
- (def sin(unary (fn[^double a] (Math/sin a))))
- (def cos (unary (fn[^double a] (Math/cos a))))
- (def BinaryActions {'+ add '- subtract '* multiply '/ divide})
- (def UnaryAction {'negate negate 'sin sin 'cos cos})
- (defn parser[string]
- (cond (number? string) (constant string)
- (symbol? string) (variable (str string))
- (seq? string)
- (if (UnaryAction (first string))
- ((UnaryAction (first string)) (parser (second string)))
- ((BinaryActions (first string)) (parser (second string)) (parser (nth string 2))))))
- (defn parseFunction[string]
- (parser (read-string string)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement