Generating infinite sequences - oliyh/learning-clojure GitHub Wiki

lazy-seq takes a body of expressions and only evaluates them when the sequence is realised.

The following function describes the number of dots required to draw a triangle of side n.

Triangles drawn with dots

(defn triangulars
  ([] (triangulars 1 0))
  ([n prev]
    (lazy-seq (cons (+ n prev) (triangulars (inc n) (+ n prev))))))

(take 4 (triangulars)) ;; (1 3 6 10)