Monads: Difference between revisions

Added Maybe Monad implementation to Clojure section. Changed the wording of lead section to make task more explicit and clear.
(J draft - task description is probably too skimpy with too much assumed to be a reasonable task for arbitrary languages - so let's illustrate that.)
(Added Maybe Monad implementation to Clojure section. Changed the wording of lead section to make task more explicit and clear.)
Line 1:
Demonstrate in your programming language the following:
 
1.# Construct a Monad (preferably the Option/Maybe Monad or the List Monad) by making bind and unit for that Monad (or just use what the language already has implemented)
2.# Make two functions, each which take a number and return a wrappedmonadic number, e.g. <code>Int -> Maybe Int</code> and <code>Int -> Maybe String</code>
3.# Compose the two functions with bind
 
=={{header|Clojure}}==
 
List===Maybe Monad:===
 
<lang clojure>
(defn bind [val f] (if (nil? (:value val)) val (f (:value val))))
(defn unit [val] {:value val})
 
(defn opt_add_3 [n] (unit (+ 3 n))) ; takes a number and returns a Maybe number
(defn opt_str [n] (unit (str n))) ; takes a number and returns a Maybe string
 
(bind (unit 4) opt_add_3) ; evaluates to {:value 7}
(bind (unit nil) opt_add_3) ; evaluates to {:value nil}
(bind (bind (unit 8) opt_add_3) opt_str) ; evaluates to {:value "11"}
(bind (bind (unit nil) opt_add_3) opt_str) ; evaluates to {:value nil}
</lang>
 
===List Monad:===
 
<lang clojure>
(defn bind [coll f] (apply vector (mapcat f coll)))
(defn unit [eval] (vector eval))
 
(defn doubler [n] [(* 2 n)]) ; takes a number and returns a List number
(def vecstr (comp vector str)) ; takes a number and returns a List string
(defn doubler [n] [n n])
 
(bind (bind [(vector 3 4 5] vecstr) doubler) vecstr) ; evaluates to ["36" "38" "4" "4" "5" "510"]
(-> [3 4 5]
(bind vecstrdoubler)
(bind doublervecstr)) ; also evaluates to ["36" "38" "4" "4" "5" "510"]
</lang>
 
Anonymous user