From: Joe Marshall
Subject: Re: combination of applications
Date: 
Message-ID: <acz7qc9r.fsf@comcast.net>
ยทยทยท@zedat.fu-berlin.de (Stefan Ram) writes:

>   For functions of one argument f, g, h, ... an application
>   might be written in Cambridge notation as follows
>
> ( f ( g ( h x )))
>
>   When this pattern occurs often and possibly to a greater
>   depth, one might wish to write it without that many
>   parentheses.  In mathematics, there is a binary operator "o"
>   to combine two functions, so that one might write
>
> ( f o g o h )( x )
>
>   Now the number of parentheses needed does not grow with the
>   number of functions combined.

(defun compose2 (f g)
  (lambda (&rest args)
    (multiple-value-call f (apply g args))))

(defun compose (f &rest more)
  (reduce #'compose2 more :initial-value f))

(funcall (compose #'sin #'1+ #'sqrt) 3)
=> 0.398189


>   Are there means in Lisp with the same effect, possibly
>   something like the following?
>
> ( combine f g h x )

(defun combine (&rest funcs-and-args)
  (apply (apply #'compose (butlast funcs-and-args)) (last funcs-and-args)))

(combine #'sin #'1+ #'sqrt 3)
=> 0.398189

-- 
~jrm