Re: Newbie question: writing a composer function in Lisp



Hi,

Code:
(defun my-compose (&rest function-list)
(if (= 1 (length function-list))
#'(lambda(x) (funcall (car function-list) x))
#'(lambda(x) (funcall (car function-list) (funcall (my-
compose (cdr function-list)) x)))))


If you change car to caar, it would work:

(defun my-compose (&rest function-list)
(if (= 1 (length function-list))
#'(lambda(x) (funcall (caar function-list) x))
#'(lambda(x) (funcall (caar function-list)
(funcall (my-compose (cdr function-list)) x)))))

But it won't be correct.

The problem is that you are using (&rest function-list), which would would put all the rest arguments in list itself, and that's why you need double car - caar.

In fact, you don't need (&rest function-list), so the correct solution is:

(defun my-compose (function-list)
(if (= 1 (length function-list))
#'(lambda(x) (funcall (car function-list) x))
#'(lambda(x) (funcall (car function-list)
(funcall (my-compose (cdr function-list)) x)))))

Thanks,
Dimiter "malkia" Stanev.
.



Relevant Pages