calrissian/include/monads.lfe

26 lines
555 B
Text
Raw Normal View History

2014-04-24 19:33:28 +00:00
(defmacro do args
(let ((monad (car args))
(statements (cdr args)))
(monad:do-transform monad statements)))
(defmacro >>= (monad m f)
`(: ,monad >>= ,m ,f))
(defmacro >> (monad m1 m2)
`(: ,monad >>= ,m1 (lambda (_) ,m2)))
(defmacro return (monad expr)
`(: ,monad return ,expr))
2014-04-25 05:06:19 +00:00
(defmacro sequence (monad list)
`(: lists foldr
(lambda (m acc) (mcons ,monad m acc))
(return ,monad [])
,list))
(defmacro mcons (monad m mlist)
`(do ,monad
(x <- ,m)
(rest <- ,mlist)
(return ,monad (cons x rest))))