Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Created January 10, 2012 20:13
Show Gist options
  • Save diegopacheco/1590930 to your computer and use it in GitHub Desktop.
Save diegopacheco/1590930 to your computer and use it in GitHub Desktop.
Monads
;
; Monads
; ======
;
; * Without monads "pure" functional programming could be very impractical, Hack!
; * You can build environments that support exactly the features that you want
; * Functional able todo IO
; * Function Composition: LINQ, Unix Pipes
; * Good for control tracking of things
; * Encapsulating two things:
; - control flow (Maybe, Error, List, Continuation, parser monads)
; - state propagation (State, Reader, Writer, IO).
;
; Sample:
;
;
;;; Monads ins Clojure 1 ;;; Function Composition
(defn triple[n](* 3 n)) ;;; function that triple a number - F1
(def inc3(comp triple inc)) ;;; F2 must produce a result that f1 can handle
(inc3 3) ;;; result is 12. R: inc 3 == 4, 4 * 3 == 12
;;; Monads ins Clojure 2 ;;; The Sequence Monad(Haskell 3 parts, types sing, 2 functions)
;;; ;;; Since clojure is dynamic type we just need 2 functions
;;; ;;; m-result and m-bind this are standard names
;;; ;;; monadic-values: (4 6) monadic-functions: increase
(defn increase[x](list (+ x 1)(+ x 2)) ;;; receive a int and create a list with int+1 and int+2
(def m-result list) ;;; create a list with a int
(defn m-bind[v f](mapcat f v)) ;;; Monadic bind call the value(list of int) into increase
(m-bind (m-result 4) increase) ;;; (5 6)
; Resources:
;
; http://marijnhaverbeke.nl/monad.html
; http://www.reddit.com/r/programming/comments/p66e/are_monads_actually_used_in_anything_except
; http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf
; http://www.reddit.com/r/programming/tb/oq3m
; http://www.infoq.com/presentations/Monads-Made-Easy
; http://www.cas.mcmaster.ca/~carette/pa_monad/
; http://www.reddit.com/r/programming/comments/ox6s/ask_reddit_what_the_hell_are_monads/coxiv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment