Anonymous user
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:
=={{header|Clojure}}==
<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 [
(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
(bind (bind
(-> [3 4 5]
(bind
(bind
</lang>
|