1
1
(ns examples.functional )
2
2
3
3
; START: stack-consuming-fibo
4
+ ; bad idea
4
5
(defn stack-consuming-fibo [n]
5
6
(cond
6
7
(= n 0 ) 0 ; <label id="code.stack-consuming-fibo.0"/>
9
10
; END: stack-consuming-fibo
10
11
11
12
; START: tail-recursive-fibo
12
- ; beware : still consumes stack!
13
+ ; also bad : still consumes stack!
13
14
(defn tail-recursive-fibo [n]
14
15
(cond
15
16
(= n 0 ) 0
23
24
; END: tail-recursive-fibo
24
25
25
26
; START: loop-recur-fibo
27
+ ; better but not great
26
28
(defn loop-recur-fibo [n]
27
29
(cond
28
30
(= n 0 ) 0
35
37
; END: loop-recur-fibo
36
38
37
39
; START: fibo-series
38
- ; returns series to n (heap-consuming!)
40
+ ; returns series to n
41
+ ; still bad (heap-consuming!)
39
42
(defn fibo-series [count]
40
43
(let [n (dec count)]
41
44
(cond
60
63
(def head-fibo (lazy-cat [0 1 ] (map + head-fibo (rest head-fibo))))
61
64
; END: head-fibo
62
65
63
- ; inspired by http://www.cs.uni.edu/~wallingf/patterns/recursion.html
64
- (defn symbol-replace [coll oldsym newsym]
65
- (if (empty? coll)
66
- ()
67
- (let [f (first coll)]
68
- (if (symbol? f)
69
- (lazy-cons (if (= f oldsym) newsym f)
70
- (symbol-replace (rest coll) oldsym newsym))
71
- (lazy-cons (symbol-replace f oldsym newsym)
72
- (symbol-replace (rest coll) oldsym newsym))))))
73
-
74
- (declare h-male h-female )
75
- (defn h-male [n]
76
- (if (= n 0 )
77
- 0
78
- (- n (h-female (h-male (dec n))))))
79
-
80
- (defn h-female [n]
81
- (if (= n 0 )
82
- 1
83
- (- n (h-male (h-female (dec n))))))
84
- (def h-male (memoize h-male))
85
- (def h-female (memoize h-female))
86
-
87
- (def h-male-seq (map h-male (iterate inc 0 )))
88
- (def h-female-seq (map h-female (iterate inc 0 )))
89
-
90
66
; START: count-heads-pairs
91
67
(defn count-heads-pairs [coll]
92
68
(loop [cnt 0 coll coll] ; <label id="code.count-heads-loop.loop"/>
110
86
(defn count-heads-pairs [coll]
111
87
(count (filter (fn [pair] (every? #(= :h %) pair))
112
88
(by-pairs coll))))
89
+ ; END: count-heads-by-pairs
113
90
(def count-heads-by-pairs count-heads-pairs )
114
- ; START: count-heads-by-pairs
115
91
116
- ( def
117
- #^{ :doc " Count items matching a predicate. "
118
- :arglists '([pred coll])}
119
- count-if ( comp count filter))
92
+ ; START: count-if
93
+ ( use '[clojure.contrib.def :only ( defvar )])
94
+ ( defvar count-if ( comp count filter) " Count items matching a filter " )
95
+ ; END: count-if
120
96
97
+ ; START: count-runs
121
98
(defn
122
99
count-runs
123
100
" Count runs of length n where pred is true in coll."
124
101
[n pred coll]
125
102
(count-if #(every? pred %) (partition n 1 coll)))
103
+ ; END: count-runs
126
104
105
+ ; START: count-heads-by-runs
106
+ (defvar count-heads-pairs (partial count-runs 2 #(= % :h ))
107
+ " Count runs of length two that are both heads" )
108
+ ; END: count-heads-by-runs
109
+ (def count-heads-by-runs count-heads-pairs )
127
110
111
+ ; START: my-odd-even
128
112
(declare my-odd? my-even? )
129
113
130
114
(defn my-odd? [n]
@@ -136,15 +120,32 @@ count-if (comp count filter))
136
120
(if (= n 0 )
137
121
true
138
122
(my-odd? (dec n))))
139
-
140
- (defn my-parity? [parity n]
141
- (if (= n 0 )
142
- (= parity 0 )
143
- (recur (- 1 parity) (dec n))))
144
-
145
- (def my-even-2? (partial my-parity? 0 ))
146
- (def my-odd-2? (partial my-parity? 1 ))
147
-
123
+ ; END: my-odd-even
124
+
125
+ ; START: parity
126
+ (defn parity [n]
127
+ (loop [n n par 0 ]
128
+ (if (= n 0 )
129
+ par
130
+ (recur (dec n) (- 1 par)))))
131
+ ; END: parity
132
+
133
+ ; START: my-odd-even-parity
134
+ (defn my-even? [n] (= 0 (parity n)))
135
+ (defn my-odd? [n] (= 1 (parity n)))
136
+ ; END: my-odd-even-parity
137
+
138
+ ; START: curry
139
+ ; almost a curry
140
+ (defn curry [f]
141
+ (fn [& front]
142
+ (fn [& back]
143
+ (apply f (concat front back)))))
144
+ ; END: curry
145
+
146
+ ; --------------------------------------------------------------------------------------
147
+ ; -- See www.cs.brown.edu/~sk/Publications/Papers/Published/sk-automata-macros/paper.pdf
148
+ ; --------------------------------------------------------------------------------------
148
149
(defn machine [stream]
149
150
(let [step {[:init 'c] :more
150
151
[:more 'a] :more
@@ -175,5 +176,4 @@ count-if (comp count filter))
175
176
(when-not (seq stream) true ))
176
177
177
178
178
-
179
-
179
+
0 commit comments