There are a couple definitions in arc.arc that benefit from being redefined using fold: (def fold (f v xs)
(if xs
(f (car xs) (fold f v (cdr xs)))
v))
The first is map1: (def map1 (f xs)
(fold (fn (x xs) (cons (f x) xs)) nil xs))
The second is best: (def best (f seq)
(fold (fn (x y) (if (f x y) x y)) (car seq) (cdr seq)))
These definitions are shorter than the originals and almost twice as fast. (Functions such as rev and len are slower with fold.)They could also be rewritten with a slightly more exotic notation using a macro that creates an anonymous two variable function: (mac xy exprs
`(fn (x y) ,@exprs))
(def map1 (f xs)
(fold (xy (cons (f x) y)) nil xs))
(def best (f seq)
(fold (xy (if (f x y) x y)) (car seq) (cdr seq)))
|