posted an update

Holy smokes!

So, I just labored through ["Pascal's Triangle" on 4clojure](].

For background, the first 5 rows of Pascal's Triangle looks something like this:

         [1 1]
       [1 2 1]
      [1 3 3 1]
     [1 4 6 4 1]

The problem calls for simply returning the nth row, not the whole triangle. I finally got a working solution:

(fn [n] 
  (map #(apply 
         (fn pas [x y] 
           (if (or (zero? x) (= x y)) 1 
             (+ (pas (dec x) (dec y)) (pas x (dec y))))) %) 
       (for [x (range n)] [x (dec n)])))

Without explaining in detail, I didn't like having to use an if/else branch and recursion.

I was really impressed to find another user build up the whole triangle without either:

(take n (iterate (fn next-row [v] (vec (map + (apply conj [0] v) (conj v 0)))) [1]))

It hadn't occurred to me to use iterate with a vector to build up the rows. Very cool!

Log in or sign up for Devpost to join the conversation.