-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathflowcontrol.clj
More file actions
626 lines (544 loc) · 14.5 KB
/
flowcontrol.clj
File metadata and controls
626 lines (544 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
;; gorilla-repl.fileformat = 1
;; **
;;; # Clojure Flow Control
;;;
;;;
;; **
;; **
;;; ## Statements vs. Expressions
;;; In Java, expressions return values, whereas statements do not.
;;;
;;; ```java
;;; // "if" is a statement because it doesn't return a value:
;;; String s;
;;; if (x > 10) {
;;; s = "greater";
;;; } else {
;;; s = "greater or equal";
;;; }
;;; obj.someMethod(s);
;;;
;;; // Ternary operator is an expression; it returns a value:
;;; obj.someMethod(x > 10 ? "greater" : "greater or equal");
;;; ```
;;;
;;; In Clojure, however, everything is an expression! _Everything_ returns a value, and a block of multiple expressions returns the last value. Expressions that exclusively perform side-effects return `nil`.
;;;
;;; ## Flow Control Expressions
;;; Accordingly, flow control operators are expressions, too!
;;;
;;; Flow control operators are composable, so we can use them anywhere. This leads to less duplicate code, as well as fewer intermediate variables.
;;;
;;; Flow control operators are also extensible via macros, which allow the compiler to be extended by user code. We won't be discussing macros today, but you can read more about them at [Clojure.org](http://clojure.org/macros), [Clojure from the Ground Up](https://aphyr.com/posts/305-clojure-from-the-ground-up-macros), or [Clojure for the Brave and True](http://www.braveclojure.com/writing-macros/), among many other places.
;;;
;;; ## `if`
;;;
;;; `if` is the most important conditional expression - it consists of a condition, a "then", and an "else". `if` will only evaluate the branch selected by the conditional.
;; **
;; @@
(str "2 is " (if (even? 2) "even" "odd"))
;; @@
;; @@
;; else expression is optional
(if (true? false) "impossible!")
;; @@
;; **
;;; ## Truthiness
;;;
;;; In Clojure, all values are "truthy" or not. The only "false" values are `false` and `nil` - all other values are "truthy".
;; **
;; @@
(if true :truthy :falsey)
;; @@
;; @@
(if (Object.) :truthy :falsey) ; objects are true
;; @@
;; @@
(if [] :truthy :falsey) ; empty collections are true
;; @@
;; @@
(if 0 :truthy :falsey) ; zero is true
;; @@
;; @@
(if false :truthy :falsey)
;; @@
;; @@
(if nil :truthy :falsey) ; nil is false
;; @@
;; @@
(if (seq []) :truthy :falsey) ; seq on empty coll is nil
;; @@
;; **
;;; ## `if` and `do`
;;;
;;; The `if` only takes a single expression for the "then" and "else". Use do to create larger blocks that are a single expression.
;;;
;;; Note that the only reason to do this is if your bodies have side effects! (Why?)
;; **
;; @@
(if (even? 5)
(do (println "even")
true)
(do (println "odd")
false))
;; @@
;; **
;;; ## `when`
;;;
;;; `when` is a one-armed `if`. It checks a condition and then evaluates any number of expressions as a body (so no `do` is required). The value of the last expression is returned. If the condition is false, nil is returned.
;;;
;;; `when` communicates to a reader that there is no "else" branch.
;; **
;; @@
(when (neg? x)
(throw (RuntimeException. (str "x must be positive: " x))))
;; @@
;; **
;;; ## `cond`
;;;
;;; `cond` is a series of tests and expressions. Each test is evaluated in order and the expression is evaluated and returned for the first true test.
;; **
;; @@
(let [x 5]
(cond
(< x 2) "x is less than 2"
(< x 10) "x is less than 10"))
;; @@
;; **
;;; ## `cond` and `else`
;;;
;;; If no test is satisfied, nil is returned. A common idiom is to use a final test of `:else`. Keywords (like `:else`) always evaluate to true so this will always be selected as a default.
;; **
;; @@
(let [x 11]
(cond
(< x 2) "x is less than 2"
(< x 10) "x is less than 10"
:else "x is greater than or equal to 10"))
;; @@
;; **
;;; ## `case`
;;;
;;; `case` compares an argument to a series of values to find a match. This is done in constant (not linear) time! However, each value must be a compile-time literal (numbers, strings, keywords, etc).
;; **
;; @@
(defn foo [x]
(case x
5 "x is 5"
10 "x is 10"))
;; @@
;; @@
(foo 10)
;; @@
;; **
;;; Unlike `cond`, `case` will throw an exception if no value matches:
;; **
;; @@
(foo 11)
;; @@
;; **
;;; ## `case` with else-expression
;;;
;;; `case` can have a final trailing expression that will be evaluated if no test matches.
;; **
;; @@
(defn foo [x]
(case x
5 "x is 5"
10 "x is 10"
"x isn't 5 or 10"))
;; @@
;; @@
(foo 10)
;; @@
;; @@
(foo 11) ;; falls into default instead of erroring
;; @@
;; **
;;; ## Iteration for Side Effects
;;; ### `dotimes`
;;; * Evaluate expresson _n_ times
;;; * Returns `nil`
;; **
;; @@
(dotimes [i 3]
(println i))
;; @@
;; **
;;; ### doseq
;;; * Iterates over a sequence
;;; * Similar to Java's for-each loop
;;; * If a lazy sequence, forces evaluation
;;; * Returns `nil`
;; **
;; @@
(doseq [n (range 3)]
(println n))
;; @@
;; **
;;; ### `doseq` with multiple bindings
;;; * Similar to nested `foreach` loops
;;; * Processes all permutations of sequence content
;;; * Returns `nil`
;; **
;; @@
(doseq [letter [:a :b]
number (range 3)] ; list of 0, 1, 2
(pr [letter number]))
;; @@
;; **
;;; ## Clojure's `for`
;;; * List comprehension, *not* a for-loop
;;; * Generator function for sequence permutation
;;; * Bindings behave like `doseq`
;; **
;; @@
(for [letter [:a :b]
number (range 3)] ; list of 0, 1, 2
[letter number])
;; @@
;; **
;;; ## Recursion
;;;
;;; ### Recursion and Iteration
;;; * Clojure provides recur and the sequence abstraction
;;; * `recur` is "classic" recursion
;;; * Closed to consumers, lower-level
;;; * Sequences represent iteration as values
;;; * Consumers can partially iterate
;;; * Reducers represent iteration as function composition
;;; * Added in Clojure 1.5, not convered here
;;;
;;; ### `loop` and `recur`
;;; * Functional looping construct
;;; * `loop` defines bindings
;;; * `recur` re-executes `loop` with new bindings
;;; * Prefer higher-order library functions
;; **
;; @@
(loop [i 0]
(if (< i 10)
(recur (inc i))
i))
;; @@
;; **
;;; ### `defn` and `recur`
;;; * Function arguments are implicit `loop` bindings
;; **
;; @@
(defn increase [i]
(if (< i 10)
(recur (inc i))
i))
;; @@
;; @@
(increase 1)
;; @@
;; **
;;; ### `recur` for recursion
;;; * `recur` must be in "tail position"
;;; * The last expression in a branch
;;; * `recur` must provide values for all bound symbols by position
;;; * Loop bindings
;;; * defn/fn arguments
;;; * Recursion via `recur` does not consume stack
;;;
;;; ## Exceptions
;;;
;;; ### Exception handling
;;; * `try`/`catch`/`finally` as in Java
;; **
;; @@
(try
(/ 2 1)
(catch ArithmeticException e
"divide by zero")
(finally
(println "cleanup")))
;; @@
;; **
;;; ### Throwing exceptions
;; **
;; @@
(try
(throw (Exception. "something went wrong"))
(catch Exception e (.getMessage e)))
;; @@
;; **
;;; ### Exceptions with Clojure data
;;; * `ex-info` takes a message and a map
;;; * `ex-data` gets the map back out
;;; * Or `nil` if not created with `ex-info`
;; **
;; @@
(try
(throw (ex-info "There was a problem" {:detail 42}))
(catch Exception e
(prn (:detail (ex-data e)))))
;; @@
;; **
;;; ### `with-open`
;; **
;; @@
(let [f (clojure.java.io/writer "/tmp/new")]
(try
(.write f "some text")
(finally
(.close f))))
;; @@
;; @@
;; Can be written:
(with-open [f (clojure.java.io/writer "/tmp/new")]
(.write f "some text"))
;; @@
;; **
;;; # LAB: Flow Control
;;;
;;; ## I am thinking of a number
;;; Let's play a number-guessing game. Define a function `check-guess` that takes two arguments:
;;; * `secret`, the number the player is trying to guess, and
;;; * `guess`, the player's most recent guess.
;;;
;;; The function should return a string:
;;; * "You win!" if the numbers are equal,
;;; * "Too low" if the guess is less than the secret, or
;;; * "Too high" if the guess is greater than the secret.
;;;
;;; Only use `if`, not `cond`.
;; **
;; @@
;; SOLUTION
(defn check-guess [secret guess]
(if (= guess secret)
"You win!"
(if (< guess secret)
"Too low"
"Too high")))
;; @@
;; **
;;; ## I am thinking of another number
;;; Repeat the previous exercise using `cond` instead of `if`.
;; **
;; **
;;;
;; **
;; @@
;; SOLUTION
(defn check-guess [secret guess]
(cond (= guess secret)
"You win!"
(< guess secret)
"Too low"
:else
"Too high"))
;; @@
;; **
;;; ## Triplicate redux
;;; Define a function `triplicate` that takes a single argument--a function `f`--and calls that function three times. Use `dotimes`.
;;;
;;; Test it with an anonymous function that prints `:hi`.
;; **
;; @@
;; SOLUTION
(defn triplicate [f]
(dotimes [i 3]
(f)))
;; @@
;; @@
(triplicate #(prn :hi))
;; @@
;; **
;;; ## Printing numbers
;;; Define a function `numbers` that takes a single argument `n` and prints all the numbers from zero to _n-1_ (inclusive), one per line.
;; **
;; @@
(defn numbers [n]
(dotimes [i n]
(println i)))
;; @@
;; **
;;; ## Counting numbers
;;; Define a function `counting` that takes a single argument `n` and prints all the numbers from one to `n` (inclusive), one per line.
;;;
;;; _Hint:_ Use `doseq` and `range`.
;; **
;; @@
(defn counting [n]
(doseq [i (range 1 (inc n))]
(println i)))
;; @@
;; **
;;; ## Garage band
;;; Let's start a band! Define a function `print-bands` that takes three arguments:
;;; * `guitars`, a vector of guitarists' names,
;;; * `basses`, a vector of bass players' names, and
;;; * `drums`, a vector of drummers' names.
;;;
;;; The function should print all the possible three-piece combinations you can make with those players, like this:
;;; ```
;;; (print-bands ["Gary" "Gus"]
;;; ["Bill" "Bob" "Buster"]
;;; ["Darrell"])
;;;
;;; ;; Gary Bill Darrell
;;; ;; Gary Bob Darrell
;;; ;; Gary Buster Darrell
;;; ;; Gus Bill Darrell
;;; ;; Gus Bob Darrell
;;; ;; Gus Buster Darrell
;;; ```
;; **
;; @@
;; SOLUTION
(defn print-bands [guitars basses drums]
(doseq [g guitars
b basses
d drums]
(println g b d)))
;; @@
;; **
;;; ## Return of the garage band
;;; Define a function `all-bands` that takes the same arguments as `print-bands`, but instead of printing all the combinations, it returns them in a sequence. Each item in the sequence should be a vector like `[guitarist bass drummer]`.
;;;
;;; _Hint:_ Use `for`.
;; **
;; @@
(defn all-bands [guitars basses drums]
(for [g guitars
b basses
d drums]
[g b d]))
;; @@
;; **
;;; ## Fizzbuzz
;;; It's everybody's favorite programming problem! Define a function `fizzbuzz` that prints the numbers from `1` to `100` (inclusive), subject to the following rules:
;;; * If the number is a multiple of three, print "Fizz";
;;; * If the number is a multiple of five, print "Buzz";
;;; * If the number is a multiple of _both_ three and five, print "FizzBuzz"; and
;;; * If the number is _not_ a multiple of either three or five, print the number.
;;;
;;; _Hint:_ The `rem` function (short for "remainder") computes the remainder of dividing two numbers, like Java's `%` operator. Clojure also has a `zero?` function to test if a number is equal to zero.
;; **
;; @@
;; SOLUTION
(defn fizzbuzz []
(doseq [i (range 1 101)]
(println (cond (and (zero? (rem i 3))
(zero? (rem i 5)))
(zero? (rem i 3))
"Fizz"
(zero? (rem i 5))
"Buzz"
:else i))))
;; @@
;; @@
;; SOLUTION
;; If you want to avoid repeating the `rem` operation.
(defn fizzbuzz []
(doseq [i (range 1 101)]
(let [fizz (if (zero? (rem i 3)) "Fizz")
buzz (if (zero? (rem i 5)) "Buzz")
number (if (not (or fizz buzz)) i)]
(println (str fizz buzz number)))))
;; @@
;; **
;;; ## Euclid's algorithm
;;; [Euclid's algorithm](http://en.wikipedia.org/wiki/Euclidean_algorithm) finds the greatest common divisor of two integers using only subtraction. In imperative pseudo-code, it looks like this:
;;;
;;; ```
;;; function gcd(A, B):
;;; do loop:
;;; if A == 0
;;; return B
;;; if B == 0
;;; return A
;;; if A > B
;;; A := A - B
;;; else
;;; B := B - A
;;; ```
;;;
;;; Define a function `gcd` that implements Euclid's algorithm. Use `recur`.
;; **
;; @@
;; SOLUTION
(defn gcd [a b]
(cond (zero? a) b
(zero? b) a
(> a b) (recur (- a b) b)
:else (recur a (- b a))))
;; @@
;; **
;;; Some tests:
;; **
;; @@
;; TESTS
(assert (= 1 (gcd 3 4)))
(assert (= 3 (gcd 3 6)))
(assert (= 3 (gcd 6 3)))
(assert (= 25 (gcd 100 25)))
(assert (= 4 (gcd 100 8)))
(assert (= 8 (gcd 16 24)))
;; @@
;; **
;;; ## Bonus: I am thinking of a number
;;; Define a function that returns another function to play the number-guessing game.
;;;
;;; That is, define a function `guessing-game` that picks a random number and returns a function that takes a single argument, the player's guess. The returned function should return the same strings as in the first exercise.
;;;
;;; _Hint:_ Clojure's `rand-int` function returns a random integer.
;; **
;; @@
;; SOLUTION
(defn guessing-game []
(let [secret (rand-int 100)]
(fn [guess]
(cond (= guess secret)
"You win!"
(< guess secret)
"Too low"
:else
"Too high"))))
;; @@
;; **
;;; ## Bonus: Binary search
;;; Use `loop` and `recur` to implement a binary search. Define a function `binary-search` that takes two arguments:
;;; * `n`, a number, and
;;; * `nums`, a sorted vector of numbers.
;;;
;;; The function should return `true` if `nums` contains `n` and `false` if it does not.
;; **
;; @@
;; SOLUTION
(defn binary-search [n nums]
(loop [start 0
end (count nums)]
(if (= start end)
false
(let [index (+ start (int (/ (- end start) 2)))
x (nth nums index)]
(cond (= n x) true
(< n x) (recur start index)
:else (recur (inc index) end))))))
;; @@
;; **
;;; And some tests:
;; **
;; @@
;; SOLUTION
(assert (false? (binary-search 4 [1 3 5 7 9])))
(assert (false? (binary-search 11 [1 3 5 7 9])))
(assert (true? (binary-search 7 [1 3 5 7 9])))
(assert (true? (binary-search 1 [1 3 5 7 9])))
(assert (true? (binary-search 9 [1 3 5 7 9])))
(assert (true? (binary-search 54 (range 0 100 2))))
(assert (true? (binary-search 98 (range 0 100 2))))
;; @@
;; **
;;;
;;; ## Navigation
;;;
;;; * [Up (Home)](/worksheet.html?filename=src/cljlab/start.clj)
;;; * [Previous (Collections)](/worksheet.html?filename=src/cljlab/collections.clj)
;;; * [Next (Namespaces)](/worksheet.html?filename=src/cljlab/namespaces.clj)
;; **