Skip to content

Instantly share code, notes, and snippets.

@luther9
Created November 18, 2021 17:46
Show Gist options
  • Save luther9/9abd90bb1c4756bb82f37e6d63f5f937 to your computer and use it in GitHub Desktop.
Save luther9/9abd90bb1c4756bb82f37e6d63f5f937 to your computer and use it in GitHub Desktop.
IO monad in Scheme
#!/usr/bin/env guile
!#
(use-modules ((srfi srfi-1) #:select (fold)))
;;; An IO monad is a function which has no parameters.
(define (io-unit x) (lambda () x))
;;; The bind function.
(define (io->>= io . funcs)
(fold (lambda (f io) (lambda () ((f (io))))) io funcs)
)
;;; The sequence function. When called with two arguments a and b, this is
;;; equivalent to (io->>= a (lambda (x) b)).
(define (io->> io . rest)
(if (null? rest)
io
(lambda ()
(io)
((apply io->> rest))
)
)
)
(define (io-repeat io runs f init)
(define (loop runs x)
(if (< runs 1)
x
(loop (1- runs) (f x (io)))
)
)
(lambda () (loop runs init))
)
(define io-done (io-unit #f))
(define (io-wrap proc) (lambda args (lambda () (apply proc args))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment