Function composition?

Started by jopython, December 18, 2011, 06:52:05 AM

Previous topic - Next topic

jopython

Is there a builtin compose function in newlisp?

Lutz

#1
No compose function, but for sure other means to achieve similar. This link talks about function factories and the first-class nature of lambda expressions in newLISP:



http://www.newlisp.org/index.cgi?Closures">http://www.newlisp.org/index.cgi?Closures



General info about differences to Common Lisp and Scheme:



http://www.newlisp.org/index.cgi?page=Differences_to_Other_LISPs">http://www.newlisp.org/index.cgi?page=D ... ther_LISPs">http://www.newlisp.org/index.cgi?page=Differences_to_Other_LISPs



Also, 'apply' has a reduce option:



http://www.newlisp.org/downloads/newlisp_manual.html#apply">http://www.newlisp.org/downloads/newlis ... html#apply">http://www.newlisp.org/downloads/newlisp_manual.html#apply



There is also a 'curry':



http://www.newlisp.org/downloads/newlisp_manual.html#curry">http://www.newlisp.org/downloads/newlis ... html#curry">http://www.newlisp.org/downloads/newlisp_manual.html#curry

Kazimir Majorinc

#2
I wrote the post "http://kazimirmajorinc.blogspot.com/2010/02/composition-of-functions-or-macros.html">Composition of functions and macros" on my blog.



((composition 'f1 ... 'fn) _ _ _) = (f1 (f2 ... (fn _ _ _)))



If there was no significant change from February 2010, you should be able to cut and paste whole post in your editor and it should work.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

jopython

#3
Thank you , Kazimir.

William James

#4
These seem to work.

;; Compose two functions.  Both accept only 1 argument.
(define (atop f g)
  (expand (lambda (a) (f (g a))) 'f 'g))

;; Compose two functions.  The first to be called accepts
;; any number of arguments.
(define (atop* f g)
  (expand
    (lambda () (f (apply g (args))))
    'f 'g))

Thanks to Kazimir Majorinc for the idea of using expand.