|
|
|
|
|
by zosima
4838 days ago
|
|
Most Lisps aren't so great with arithmetic. Afterall arithmetic notation has been refined through 100's of years to be readable and concise, and I've never seen anybody use prefix notation for operators voluntarily. But infix notation can be added easily: this is a quick'n dirty solution in Clojure with left associativity, no precedence and many gotchas. Incanter has a much better implementation called $= with less gotchas and many more features: (defmacro arith [& args]
(letfn [(res [v]
(cond
(vector? v) (apply arith-body v)
(list? v) (map res v)
:else v))
(arith-body [s & args]
(reduce (fn ([expr] expr) ([expr [op arg]] `(~op ~expr ~(res arg))))
(res s)
(partition 2 args)))]
(apply arith-body args)))
(defn deriv [f]
(let [dx 0.001]
(fn [x] (arith
(f [x + dx]) - (f x) / dx))))
((deriv (fn [x] (arith x * x))) 3)
Another common problem is the piping of results from one function to the next: (f
(g
(h a)))
where you usually want to start reading the code in the lower rightmost corner and go upwards towards the left. Very messy in many lisps.Clojure has solved that problem with the threading macros, yielding postfix notation when you need it the most: (-> a h g f)
Piping subresults between functions really doesn't get much better in any language.And that's of course some of the appeal of Lisp: The syntax may start controversial, but you can choose most of it yourself and make it depend on the problem, your tastes and of course, your readers. |
|
Infix notation is provided by macros or read macros.
Infix notation is provided by math packages on top of Lisp: Macsyma, Reduce, Derive, Axiom, ...
Those provide REAL extensive math syntax capabilities, not just what typical programming languages provide.