Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn operationFactory [operator]
- (fn operation [leftExpr rightExpr]
- (fn [args]
- (operator (leftExpr args)
- (rightExpr args))
- )))
- (defn unaryOperationFactory [operator]
- (fn operation [Expr]
- (fn [args]
- (operator (Expr args))
- )))
- (defn constant [val]
- (fn [args] (double val)))
- (defn variable [name]
- (fn [args] (args name)))
- (def multiply (operationFactory *))
- (def add (operationFactory +))
- (def subtract (operationFactory -))
- (defn divide [leftExpr rightExpr]
- (fn [args]
- (/ (double (leftExpr args))
- (double (rightExpr args)))
- ))
- (def cosh (unaryOperationFactory 'Math/cosh))
- (def negate (unaryOperationFactory -))
- (def sinh (unaryOperationFactory 'Math/sinh))
- (def sin (unaryOperationFactory 'Math/sin))
- (def cos (unaryOperationFactory 'Math/cos))
- (def op {'+ add, '- subtract, '* multiply, '/ divide,
- 'sinh sinh, 'cosh cosh, 'sin sin, 'cos cos, 'negate negate
- })
- (defn doParseFunction [expression]
- (cond
- (seq? expression) (apply (op (first expression)) (map doParseFunction (rest expression)))
- (number? expression) (constant expression)
- (symbol? expression) (variable (str expression))
- )
- )
- (defn parseFunction [expression]
- (doParseFunction (read-string expression))
- )
- (def expr (parseFunction "(sinh (- x y))"))
- (sinh (subtract (variable "x") (variable "y")))
- (print (expr {"z" 0.0, "x" 0.0, "y" 0.0}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement