2013-05-28 73 views
1

這裏是我嘗試在elisp中實現Maybe monad。它在第一個零上打破它的功能組合。但是value總是13。我的錯誤在哪裏?也許monis在elisp

(defun .compose-maybe (&rest funcs) 
    "Monad Maybe function composition with nil as Nothing." 
    (lambda (arg) 
    (if funcs 
     (let ((value (funcall (apply #'.compose-maybe (cdr funcs)) arg))) 
      (message "%s" value) 
      (when value (funcall (car funcs) value)))) 
     arg)) 

(funcall (.compose-maybe (lambda (x) (* x 5)) (lambda (x) (+ 100 x))) 13) 

回答

2

arg瀑布(if funcs ...)的bouds之外,從而在.compose-maybe內拉姆達總是返回arg而不是隻有當funcsnil

(defun .compose-maybe (&rest funcs) 
    "Monad Maybe function composition with nil as Nothing." 
    (lambda (arg) 
    (if funcs 
     (let ((value (funcall (apply #'.compose-maybe (cdr funcs)) arg))) 
      (message "%s" value) 
      (when value (funcall (car funcs) value))) 
     arg)))