paul-graham

Compose example in Paul Graham's ANSI Common Lisp

北慕城南 提交于 2019-12-03 16:27:01
问题 Can anybody explain an example in Paul Graham's ANSI Common Lisp page 110? The example try to explain the use &rest and lambda to create functional programming facilities. One of them is a function to compose functional arguments. I cannot find anything explaining how it worked. The code is as follows: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The usage is:

Compose example in Paul Graham's ANSI Common Lisp

百般思念 提交于 2019-12-03 05:41:36
Can anybody explain an example in Paul Graham's ANSI Common Lisp page 110? The example try to explain the use &rest and lambda to create functional programming facilities. One of them is a function to compose functional arguments. I cannot find anything explaining how it worked. The code is as follows: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The usage is: (mapcar (compose #'list #'round #'sqrt) '(4 9 16 25)) The output is: ((2) (3) (4) (5)) Line 2 and 6 look

Writing an accumulator function in Clojure

∥☆過路亽.° 提交于 2019-12-01 04:10:27
I'd like to know how to write the accumulator example included in the Revenge of the Nerds essay. It's easy to understand how it works, however I fail to recreate it in Clojure - it doesn't accumulate but just returns the sum of i and the initial given value of n. The key is in incf (in the Common Lisp version) or += (in JavaScript). In other words: how to alter the state of a referenced function? I've seen some examples on mutating variables but they don't look precisely pretty do they? Don't doooo itttttt! Save yourself before it's too late! Mutating state for no reason is not something

Writing an accumulator function in Clojure

徘徊边缘 提交于 2019-12-01 01:25:34
问题 I'd like to know how to write the accumulator example included in the Revenge of the Nerds essay. It's easy to understand how it works, however I fail to recreate it in Clojure - it doesn't accumulate but just returns the sum of i and the initial given value of n. The key is in incf (in the Common Lisp version) or += (in JavaScript). In other words: how to alter the state of a referenced function? I've seen some examples on mutating variables but they don't look precisely pretty do they? 回答1: