public
Description: Various programming language explorations
Homepage: http://fogus.me
Clone URL: git://github.com/fogus/polyglot.git
polyglot / reading / onlisp / chapter3.clj
100644 35 lines (21 sloc) 1.092 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
; Chapter 3 was a relatively short chapter (code-wise). It dealt mostly with side-effects and the virtues of a more functional style to avoid such evil. Thankfully, Clojure eliminates many of the problems outlined by Graham in the way that it prefers immutability and minimizes the functions causing side-effects.
 
; pg. 29
 
; Graham provides an intentionally bad implementation of a reverse function that modifies a list in place. To do this in Clojure would require some gymnastics and therefore is probably not worth the effort.
 
; pg. 30
 
;<pre lang="lisp">
 
(defn good-reverse [lst]
  (defn rev [lst acc]
    (if (nil? lst)
      acc
      (rev (rest lst) (cons (first lst) acc))))
  (rev lst nil))
 
;</pre>
 
; pg. 32
 
; Clojure doesn't provide multiple value bindings in the same way that Lisp does. Instead, you can construct a vector of values and then deconstruct them easily on the return within a let)
 
;<pre lang="lisp">
 
(defn mytrunc [num]
  [(int num) (- num (int num))])
 
(let [[int_part frac_part] (mytrunc 26.21875)]
  (str int_part " and " frac_part))
 
;</pre>
 
; -m