(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.
@stopachka How about that? ^
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.
Amaazing!
also concat is more forgiving about doesn't have subtle bug possibilities via seq / vector conj location