I wish to create a function that can compose two functions into one. Given g
and f
, it should create a function h
provides the output of f
as input to g
. Here was my result:
(define (compose g f)
(lambda (.args)
(g (apply f (if (atom? .args) (list .args) .args)))))
The following snippet works with compose
.
(display ((compose add1 add1) 3))
However, I cannot figure out why the example below won't work. I provided the error message that I got below, and I am using Chez Scheme 9.5.2. The error here probably has to do with the variadic nature of compose
, but that's just an idea. Does anyone know why my second test isn't working as expected?
(define (reduce f lst seed)
(if (null? lst) seed
(f (car lst) (reduce f (cdr lst) seed))))
(define product-of-incremented-list
(compose
(lambda (lst) (reduce * lst 1))
(lambda (lst) (map add1 lst))))
(display (product-of-incremented-list '(3 4 5)))
; I was expecting 120, because (3 4 5) -> (4 5 6) and (4 5 6) -> 120
; I'm getting an error instead:
; Exception: incorrect number of arguments to #<procedure at compose.scm:285>