-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathscip.clj
More file actions
39 lines (26 loc) · 1.18 KB
/
scip.clj
File metadata and controls
39 lines (26 loc) · 1.18 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
(ns clojure-through-code.scip)
;; Pascals Triange
;;
(defn next-row
[last-row]
(let [sum-neighbor (map-indexed (fn [idx x]
(+ x (get last-row (+ 1 idx) 0)))
last-row)]
(into [1] sum-neighbor)))
(defn pascal
[n]
(condp = n
0 []
1 [[1]]
(let [prev-p (pascal (- n 1))]
(conj prev-p (next-row (last prev-p))))))
;; Would be curious to hear if you think there's a more idiomatic clojure way to do this : }
;; seancorfield 01:08
(defn pascal
[n]
(take n (iterate #(mapv (fn [[x y]] (+ x y))
(partition 2 1 (cons 0 (into % [0])))) [1])))
;; For a given row, if you prepend and append 0 and then break into (overlapping) pairs, then add each pair, you get the next rows.
;; So you can have an infinite sequence of Pascal's triangle rows and you just take as many rows as you want.
;; You could safely use (concat [0] % [0]) instead of (cons 0 (into % [0])) if you find that clearer -- mapv is eager so you won't get a stack overflow from a lazy computation.
;; concat (is more forgiving about) doesn't have subtle bug possibilities via seq / vector conj location (edited)