Tuesday, September 18, 2012

Purely functional term rewriting

Unlike the usually side effect inducing operation eval, term reduction is an idempotent and purely functional operation that reduces a set of terms to canonical form. Macroexpansion is essentially the first phase in term reduction:
(clojure.walk/macroexpand-all 
 '(-> [4 3 2 1] rest reverse))
The macroexpansion described in the above expression yields:
(reverse (rest [4 3 2 1]))
The next stage of term reduction is to apply the pure functions first and reverse to get the list (1 2 3). However, Clojure doesn't have any advanced facilities of term reduction to achieve that effect and even macroexpand-all has to be brought in from a separate module outside of the core.

No comments:

Post a Comment