1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | (defn cartesian-product [a b] (for [y b] (list* a y))) (defn number ([n] (number n n)) ([n max] ;(println "Calling number : " n " max : " max) (cond (<= n 0) [] (= n 1) ['(1)] :else (loop [i max result (if (<= n max) [(list n)] [])] (if (< i 1) result (let [next (- n i)] ;(println "result : " result " n: " n " max: " max " i: " i " next: " next) (recur (dec i) (lazy-cat result (cartesian-product i (number next (min max i))))))))))) (def number (memoize number)) |
Direct link: https://paste.plurk.com/show/198445