Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn abstractOperation [firstFunction secondFunction & arguments]
- (fn [VARS]
- (firstFunction secondFunction (map #(% VARS) arguments))))
- (defn abstractUnaryOperation [function argument]
- (fn [vars]
- (function (argument vars))))
- (defn variable [name] (fn [vars] (vars name)))
- (defn constant [arg] (constantly arg))
- (def divide (partial abstractOperation reduce #(/ %1 (double %2))))
- (def multiply (partial abstractOperation apply *))
- (def subtract (partial abstractOperation apply -))
- (def add (partial abstractOperation apply +))
- (def negate (partial abstractUnaryOperation -))
- (def square (partial abstractUnaryOperation #(* % %)))
- (def sqrt (partial abstractUnaryOperation #(Math/sqrt (Math/abs %))))
- (def variables
- {'x (variable "x")
- 'y (variable "y")
- 'z (variable "z")})
- (def operations
- {'+ add
- 'negate negate
- '- subtract
- '* multiply
- '/ divide
- 'square square
- 'sqrt sqrt})
- (defn parse [expression]
- (cond
- (list? expression)
- (apply (operations (first expression))
- (map parse (rest expression)))
- (number? expression) (constant expression)
- (contains? variables expression) (variables expression)
- (contains? operations expression) (operations expression)))
- (defn parseFunction [expression]
- (parse (read-string expression)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement