This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (generator p) | |
(set! gc #f) ; Continuation in generator | |
(lambda () | |
(call/cc (lambda (return) | |
(if gc | |
(gc #f) | |
(p (lambda (value) | |
(call/cc (lambda (cgc) | |
(set! gc cgc) | |
(return value)))))))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://underscore.io/blog/posts/2015/04/23/deriving-the-free-monad.html | |
// Example of implementing a specific instance of a free monad (for getting and | |
// setting an integer value) in TypeScript. The type system is not expressive | |
// enough to express free monads in general, but we can express a specific | |
// instance. | |
type AtomicOperation<T> = ["get", (x: number) => T] | ["set", number, T]; | |
type Program<T> = ["return", T] | ["suspend", AtomicOperation<Program<T>>]; |