Functional programming 也许是elisp中的monad

Functional programming 也许是elisp中的monad,functional-programming,elisp,monads,emacs24,Functional Programming,Elisp,Monads,Emacs24,下面是我在elisp中实现Maybe monad的尝试。它是在第一个零上中断的函数组合。但是值始终为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 fu

下面是我在elisp中实现Maybe monad的尝试。它是在第一个零上中断的函数组合。但是
值始终为
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)

您的
arg
不在
(如果funcs…
)的范围内,因此
.compose中的内部lambda可能总是返回
arg
,而不是仅当
funcs
nil
时返回

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