compose 코드

CL-USER> (defun compose (&rest fns)
           (destructuring-bind (fn1 . rest) (reverse fns)
             #'(lambda (&rest args)
                 (reduce #'(lambda (v f) (funcall f v))
                         rest
                         :initial-value (apply fn1 args)))))
COMPOSE
CL-USER> (mapcar (compose #'list #'round #'sqrt)
                 '(4 9 16 25))
((2) (3) (4) (5))

Dylan에는 기본으로 함수 합성에 써먹는 compose 함수가 있다.
(Dylan은 대략 Pascal 문법을 가지고 있다.
Scheme과 CL의 혼혈아랄까-.-...)

물론 compose는 몇 줄 쓰면 간단하게 만들 수 있다.
(ANSI Common Lisp 110쪽 function builders 항목 참조)

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://xeraph.com/tb/1228348 [도움말]

덧글

댓글 입력 영역