post02_monads.skr 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. (define (scheme-monad-link)
  2. (anchor [article]
  3. "http://okmij.org/ftp/Scheme/monad-in-Scheme.html"))
  4. (define (guile-monad-link)
  5. (anchor [package]
  6. "http://git.savannah.gnu.org/cgit/guix.git/tree/guix/monads.scm"))
  7. (post
  8. :title "Monads in Scheme!?"
  9. :date (make-date* 2016 9 12)
  10. :tags '("monads" "resource" "scheme" "guile" "haskell")
  11. (h4 [TLDR: Canonical scheme-monad ,(scheme-monad-link);
  12. A guile-monad ,(guile-monad-link) exists!])
  13. (h3 [Some Motivation])
  14. (p [Hi all, if you are like me, you
  15. started trying out Haskell, and were immediately confronted with
  16. the ominous IOMonad.
  17. To get by, I simply just remembered to use the <- syntax for output,
  18. and that Monads were Haskell's way to model imperative programming
  19. in a fully functional domain.])
  20. (p [But learning Haskell -> leaning monads, so I soon wanted to look under
  21. the hood and implement one.
  22. I parsed through quite a few monad introductions/tutorials.
  23. Turns out monads are a construct in category theory that
  24. implements the functions bind (<<=) and return. Soon enough I had
  25. written my first monad. I didn't really understand what I was doing
  26. though, mostly because Haskell monads
  27. are surrounded by ample sugar, which is good for fast development,
  28. but didn't help me picture its lambda calculus representation.
  29. The tutorials didn't help much here either.])
  30. (p [Of course, I could have given Haskell more time, but I stumbled upon a simple
  31. and elucidating ,(scheme-monad-link) using monads in Scheme!])
  32. (p [I highly recommend reading the source. The first half
  33. implements a monad using basic lambda expressions, and the second
  34. half develops the syntactic sugar to make working with monads
  35. like working with standard procedures. It then uses these forms to create
  36. a really cool binary tree that tags each node with a unique
  37. value, without exposing the tagging system to the user. If you
  38. haven't seen something like this before, it will honestly blow your
  39. mind.])
  40. (h3 [Monad support in Scheme?])
  41. (p [It's nice seeing a monad implemented in a simple language like Scheme,
  42. since its basic implementation, and syntactic extension are
  43. easy to read, and not a built-in language feature.
  44. Other than for learning though, is it worth having monads in Scheme?
  45. It is already imperative, and has simple patterns for IO, unlike
  46. Haskell, which
  47. requires them. Yet after reading the article above,
  48. monads are an elegant choice for solving certain types of problems.
  49. Only problem is, no one has implemented a basic monad library.])
  50. (h3 [Monad package in Scheme!?])
  51. (p [That was until while working on a Guix package, I stumbled on a pretty
  52. amazing monad library hidden within its core ,(guile-monad-link). Thank you
  53. civodul from #guix for writing this! It's def worth reading; it
  54. implements some of the popular monads from Haskell.])
  55. (p [As I continue to use Scheme, I hope to take advantage of and add to this library.
  56. Look forward to a standalone monad package!]))