Monads: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(redirect to Category:Monads)
 
(24 intermediate revisions by 5 users not shown)
Line 1: Line 1:
#REDIRECT [[:Category:Monads]]
Demonstrate in your programming language the following:

1. Construct a Monad (preferably the Option/Maybe Monad)
2. Make two functions, each which take a number and return a wrapped number
3. Compose the two functions with bind

=={{header|Clojure}}==

List Monad:

<lang clojure>
(defn bind [coll f] (apply vector (mapcat f coll)))
(defn unit [e] (vector e))

(def vecstr (comp vector str))
(defn doubler [n] [n n])

(bind (bind [3 4 5] vecstr) doubler) ; evaluates to ["3" "3" "4" "4" "5" "5"]
(-> [3 4 5]
(bind vecstr)
(bind doubler)) ; also evaluates to ["3" "3" "4" "4" "5" "5"]
</lang>

Latest revision as of 03:29, 1 February 2016

Redirect to: