也许在elisp的monad

时间:2013-05-28 13:32:45

标签: functional-programming elisp monads emacs24

这是我尝试在elisp中实现Maybe monad。它的功能组成在第一个零打破。但value始终为13。我的错误在哪里?

(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)

1 个答案:

答案 0 :(得分:2)

您的arg超出了(if funcs ...)的范围,因此.compose-maybe中的内部lambda始终返回arg,而不仅仅是funcs为{{} 1}}。

nil