aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/monads/examples.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/clojure/contrib/monads/examples.clj')
-rw-r--r--src/clojure/contrib/monads/examples.clj50
1 files changed, 25 insertions, 25 deletions
diff --git a/src/clojure/contrib/monads/examples.clj b/src/clojure/contrib/monads/examples.clj
index 54748d74..7c11fbc2 100644
--- a/src/clojure/contrib/monads/examples.clj
+++ b/src/clojure/contrib/monads/examples.clj
@@ -25,19 +25,19 @@
; Monad comprehensions in the sequence monad work exactly the same
; as Clojure's 'for' construct, except that :while clauses are not
; available.
-(domonad sequence
+(domonad sequence-m
[x (range 5)
y (range 3)]
(+ x y))
; Inside a with-monad block, domonad is used without the monad name.
-(with-monad sequence
+(with-monad sequence-m
(domonad
[x (range 5)
y (range 3)]
(+ x y)))
-(domonad sequence
+(domonad sequence-m
[x (range 5)
y (range (+ 1 x))
:when (= (+ x y) 2)]
@@ -45,7 +45,7 @@
; An example of a sequence function defined in terms of a lift operation.
; We use m-lift2 because we have to lift a function of two arguments.
-(with-monad sequence
+(with-monad sequence-m
(defn pairs [xs]
((m-lift 2 #(list %1 %2)) xs xs)))
@@ -55,14 +55,14 @@
; a sequence of monadic values and returns a monadic value containing
; the sequence of the underlying values, obtained from chaining together
; from left to right the monadic values in the sequence.
-(with-monad sequence
+(with-monad sequence-m
(defn pairs [xs]
(m-seq (list xs xs))))
(pairs (range 5))
; This definition suggests a generalization:
-(with-monad sequence
+(with-monad sequence-m
(defn ntuples [n xs]
(m-seq (replicate n xs))))
@@ -70,13 +70,13 @@
(ntuples 3 (range 5))
; Lift operations can also be used inside a monad comprehension:
-(domonad sequence
+(domonad sequence-m
[x ((m-lift 1 (partial * 2)) (range 5))
y (range 2)]
[x y])
; The m-plus operation does concatenation in the sequence monad.
-(domonad sequence
+(domonad sequence-m
[x ((m-lift 2 +) (range 5) (range 3))
y (m-plus (range 2) '(10 11))]
[x y])
@@ -89,7 +89,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Maybe monad versions of basic arithmetic
-(with-monad maybe
+(with-monad maybe-m
(def m+ (m-lift 2 +))
(def m- (m-lift 2 -))
(def m* (m-lift 2 *)))
@@ -99,7 +99,7 @@
; is attempted. It is best defined by a monad comprehension with a
; :when clause:
(defn safe-div [x y]
- (domonad maybe
+ (domonad maybe-m
[a x
b y
:when (not (zero? b))]
@@ -107,7 +107,7 @@
; Now do some non-trivial computation with division
; It fails for (1) x = 0, (2) y = 0 or (3) y = -x.
-(with-monad maybe
+(with-monad maybe-m
(defn some-function [x y]
(let [one (m-result 1)]
(safe-div one (m+ (safe-div one (m-result x))
@@ -121,7 +121,7 @@
; In the maybe monad, m-plus selects the first monadic value that
; holds a valid value.
-(with-monad maybe
+(with-monad maybe-m
(m-plus (some-function 2 0) (some-function 2 -2) (some-function 2 3)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -185,7 +185,7 @@
; In the first version, we call rng 12 times explicitly and calculate the
; shifted sum in a monad comprehension:
(def gaussian1
- (domonad state
+ (domonad state-m
[x1 rng
x2 rng
x3 rng
@@ -211,7 +211,7 @@
; More precisely, we need (m-lift 2 +), because we want both arguments
; of + to be lifted to the state monad:
(def gaussian2
- (domonad state
+ (domonad state-m
[sum12 (reduce (m-lift 2 +) (replicate 12 rng))]
(- sum12 6.)))
@@ -222,7 +222,7 @@
; We can also do the subtraction of 6 in a lifted function, and get rid
; of the monad comprehension altogether:
-(with-monad state
+(with-monad state-m
(def gaussian3
((m-lift 1 #(- % 6.))
(reduce (m-lift 2 +) (replicate 12 rng)))))
@@ -234,7 +234,7 @@
; For a random point in two dimensions, we'd like a random number generator
; that yields a list of two random numbers. The m-seq operation can easily
; provide it:
-(with-monad state
+(with-monad state-m
(def rng2 (m-seq (list rng rng))))
; Let's test it:
@@ -243,7 +243,7 @@
; fetch-state and get-state can be used to save the seed of the random
; number generator and go back to that saved seed later on:
(def identical-random-seqs
- (domonad state
+ (domonad state-m
[seed (fetch-state)
x1 rng
x2 rng
@@ -261,7 +261,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; A basic logging example
-(domonad (writer accu/empty-string)
+(domonad (writer-m accu/empty-string)
[x (m-result 1)
_ (write "first step\n")
y (m-result 2)
@@ -292,7 +292,7 @@
; vector accumulator. We can then place calls to write in between the
; steps, and obtain as a result both the return value of the function
; and the accumulated trace values.
-(with-monad (writer accu/empty-vector)
+(with-monad (writer-m accu/empty-vector)
(defn fib-trace [n]
(if (< n 2)
@@ -329,20 +329,20 @@
; lead immediately to a nil result in the output.
; First we define the combined monad:
-(def seq-maybe (maybe-t sequence))
+(def seq-maybe-m (maybe-t sequence-m))
; As a first illustration, we create a range of integers and replace
; all even values by nil, using a simple when expression. We use this
; sequence in a monad comprehension that yields (inc x). The result
; is a sequence in which inc has been applied to all non-nil values,
; whereas the nil values appear unmodified in the output:
-(domonad seq-maybe
+(domonad seq-maybe-m
[x (for [n (range 10)] (when (odd? n) n))]
(inc x))
; Next we repeat the definition of the function pairs (see above), but
; using the seq-maybe monad:
-(with-monad seq-maybe
+(with-monad seq-maybe-m
(defn pairs-maybe [xs]
(m-seq (list xs xs))))
@@ -369,7 +369,7 @@
; a function that behaves in the same way, passing 3 to its function
; argument. run-cont executes a continuation by calling it on identity.
(run-cont
- (domonad cont
+ (domonad cont-m
[x (m-result 1)
y (m-result 2)]
(+ x y)))
@@ -381,7 +381,7 @@
(def continuation nil)
(run-cont
- (domonad cont
+ (domonad cont-m
[x (m-result 1)
y (call-cc (fn [c] (def continuation c) (c 2)))]
(+ x y)))
@@ -397,7 +397,7 @@
(defn sqrt-as-str [x]
(call-cc
(fn [k]
- (domonad cont
+ (domonad cont-m
[_ (m-when (< x 0) (k (str "negative argument " x)))]
(str (. Math sqrt x))))))