Cannot cast clojure.lang.LazySeq to java.lang.Number

while solving one of the problems of 4clojure i hit the wall with this exception :
code is :
;// pascal triangle.
((memoize (fn Pascal [x]
                (if (= x 1)
                  [1]
                  (let [abRow (Pascal (- x 1))]
                    (concat [1]
                            (map + (partition 2 1 abRow)) [1]))))
       ) 3 )

This expected output is the third (3) row of the pascal triangle i.e. (1 2 1)
but it was giving
ClassCastException Cannot cast clojure.lang.LazySeq to java.lang.Number  java.lang.Class.cast (:-1)
 
Can you find the error ? ? ?

What does this code do ?
It calculates the pascal row of the (n-1)th row called abRow. Then it creates a new list
which partition the abRow in sets of 2 at each step. So, like for '(1 2 1) the partition code
(partition 2 1 '(1 2 1)) would give ((1 2) (2 1)). we then add each of the element of this list.

Spoilers:

Lets break the code and unit test it...
which is so easy in clojure :
lets take
(partition 2 1 abRow)
we can place '(1 1) in place of abRow.
so (partition 2 1 '(1 1)) gives me '((1 1)) which is expected.
so this part is fine.
lets check (map + '((1 1)))
same exception so we know that this is the problem.
right + takes two arguments but map requires function taking only one argument.
so changing the + to #(+ (first %) (second %))
and code to
;// pascal triangle.
((memoize (fn Pascal [x]
                (if (= x 1)
                  [1]
                  (let [abRow (Pascal (- x 1))]
                    (concat [1]
                            (map #(+ (first %) (second %)) (partition 2 1 abRow)) [1]))))
       ) 3 )

gives right answer :)

Comments