3 ms·
I haven't actually learned monads yet, but am I correct that the end of this article leaves readers a hair's breadth away from an introduction to monads (and wi
by minopret 14y ago
I haven't actually learned monads yet, but am I correct that the end of this article leaves readers a hair's breadth away from an introduction to monads (and without another round of distracting analogies)?
And that reminds me, lately I have been kicking around a suspicion that, given the right example, it wouldn't be hard to understand how to use call/cc to implement a lazy function in Scheme--perhaps one that returns a lazy list of the natural numbers.
As a step in that direction, I managed to construct the following example, which I hope is pretty clear.
; Return anon function returning its argument's value at n: _(n).
(define (at n) (lambda (s) (s n)))
; The curried (schönfinkeled) function t(m, n) = 2m+n: (t m) returns anon function 2m+_.
(define (t m) (lambda (n) (+ (* 2 m) n)))
; Example of using these "at" and "t" together.
(define (f n m) ((at n) (t m))) (display (f 5 3)) (newline); 11
(display (f 3 5)) (newline) ; 13
; Write an equivalent function inside-out using call/cc!
(define (g n m) ((t m) (call/cc (at n)))) (display (g 5 3)) (newline) ; 11
(display (g 3 5)) (newline) ; 13
- mattmight 14y agoYou're absolutely right that the end of the article is a hair's breadth away from monads. In fact, monads were secretly used throughout the article in the form of the comprehension notation (which desugars to the List monad). There's also a tight connection between continuation-passing style, continuations and monads. More on that in a future post...
- lucian1900 14y agoNot particularly, no. While all of the operations described can be generalised to monads, they are not specific to them in any way.