Functional programming 也许是elisp中的monad
下面是我在elisp中实现Maybe 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
值始终为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)))