Prev: Re^2: slime errors in cmucl?
Next: Land of Lisp
From: Slobodan Blazeski on 31 Mar 2010 14:52 On Mar 23, 7:55 am, Paul Donnelly <paul-donne...(a)sbcglobal.net> wrote: > Hugh Aguilar <hughaguila...(a)yahoo.com> writes: > > Another funky word is "curry." This seems to refer to nothing more > > than providing default values for input parameters, which has always > > been available in C++. > > Providing them after the function has been written, however, and > reducing its arity. If you want to map a function over a list and add 4 > to each item, you can produce an adder bycurryingthe + function, > without changing its definition or typing out the whole deal. You can > curry the curried function too, if you've got nested iterations. It's > particularly handy in functional programming, since mapping and > higher-order functions are the main tools. In Factor as well, since > quotations and combinators are used instead of loops structures that > update variables and have bodies. > > (defun curry (function &rest args) > (lambda (&rest more-args) > (apply function (append args more-args)))) > > (mapcar (curry #'+ 4) (list 1 2 3 4)) => (5 6 7 8) > > Strictly speaking, this use of the term is odd, and partial application > is more appropriate.Currying, as I understand it, is a transformation > like this: > > (lambda (x y z) (+ x y z)) > > (lambda (z) > (lambda (y) > (lambda (x) > (+ x y z)))) > > Or maybe with the nesting reversed; I don't know. But the term is > generally used to mean what the curry function above does. There's a cool joy(*) combinator called cleave, its useful when several expressions need the same value but you don't want to introduce new variable a.k.a tacit(**) style. And I just defined it as below : (defmacro cleave (outer value &rest args) (let ((g (gensym))) `(let ((,g ,value)) (,outer ,@(mapcar (lambda (x) (list x g)) args))))) (defun avg (&rest args) (cleave / args (lambda (x) (reduce #'+ x)) length)) CL> (avg 12 3 4 5) 6 Unfortunately it didn't worked with your curry so I rewrote it into: (defmacro cleave (outer value &rest args) (let ((g (gensym))) `(let ((,g ,value)) (,outer ,@(mapcar (lambda (x) (if (symbolp x) (list x g) (list 'funcall x g))) args))))) CL> (cleave / '(1 2 3 4) (curry #'reduce #'+) length) 5/2 Ugly but it seems to work. Anyway thanks for the bug report. cheers Slobodan (*) http://en.wikipedia.org/wiki/Joy_(programming_language) (**) http://en.wikipedia.org/wiki/Tacit_programming
|
Pages: 1 Prev: Re^2: slime errors in cmucl? Next: Land of Lisp |