Skip to content

Instantly share code, notes, and snippets.

@Sgeo
Created September 18, 2014 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sgeo/4a8cd6bf8a2ff09e9928 to your computer and use it in GitHub Desktop.
Save Sgeo/4a8cd6bf8a2ff09e9928 to your computer and use it in GitHub Desktop.
#lang racket/base
(require racket/control)
(struct monad (bind return) #:transparent)
(define-syntax-rule (with-monad ([val m]) body ...)
(let* ([m-once m]
[val
(lambda (ma)
(shift k
((monad-bind m-once) ma k)))])
(reset
((monad-return m-once)
body ...))))
(define list-m (monad (lambda (ma f) (apply append (map f ma))) list))
(display (with-monad ((val list-m))
(+ (val (list 1 2 3)) (val (list 4 5 6)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment