Questions I have

Is a memoized function pure? (For sure it is referentially transparent, since the result for the same arguments won't change. However, it does have some side effect upon the first call, doesn't it, so I'm not sure whether it is pure or not...)

Order of cons in tri*
In the Peg Thing Example, tri* is defined as follows:
(defn tri*
  "Generates lazy sequence of triangular numbers"
  ([] (tri* 0 1))
  ([sum n]
     (let [new-sum (+ sum n)]
       (cons new-sum (lazy-seq (tri* new-sum (inc n)))))))
What I don't understand: why does the lazy sequence end up in the right order? cons inserts at the beginning...

No comments:

Post a Comment