Task

Create a function, compose,   whose two arguments   f   and   g,   are both functions with one argument.



The result of compose is to be a function of one argument, (lets call the argument   x),   which works like applying function    f    to the result of applying function    g    to    x.



Example

 compose(f, g) (x) = f(g(x))



Reference: Function composition

Hint: In some languages, implementing compose correctly requires creating a closure.




 
(defun compose (f g)
  (lambda (x)
    (funcall f
      (funcall g x))))
 
(defun compose (funcs)
  (lists:foldl #'compose/2
               (lambda (x) x)
               funcs))
 
(defun check ()
  (let* ((sin-asin (compose #'math:sin/1 #'math:asin/1))
         (expected (math:sin (math:asin 0.5)))
         (compose-result (funcall sin-asin 0.5)))
    (io:format '"Expected answer: ~p~n" (list expected))
    (io:format '"Answer with compose: ~p~n" (list compose-result))))
 