; Can you combine an S-expr and a thunk to make a stream?
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 9
; Exercise 9
; Common Lisp
; http://t...content-available-to-author-only...r.com/thelittlelisper
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper-chapter-9-lamdba-ultimate.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------

(defun add1 ()
    (lambda (n)
      (+ 1 n)))
      
(setf zero 0)

(defun zero (n)
  (cond
   ((null n) '())
   ((= zero n) t)
   (t '())))

(defun sub1 (n)
  (cond
   ((null n) '())
   ((- n 1))))


(print (funcall (add1) 1))
;2

(defun build (a b)
  (cons a (cons b '())))

(defun str-maker (next n)
  (build n (lambda () (str-maker next (funcall (funcall next) n)))))

(defun int_ () (str-maker 'add1 0))

(defun add2 ()
  (lambda (n)
    (+ 2 n)))

(print (funcall (add2) 1))
;3

(defun even () (str-maker 'add2 0))

(defun odd () (str-maker 'add2 1))

(defun frontier (str n)
    (cond
     ((zero n) '())
     (t (cons (first  (funcall str)) (frontier (second (funcall str)) (sub1 n))))))

(print (frontier #'int_ 10))
;(0 1 2 3 4 5 6 7 8 9)

(print (frontier #'int_ 100))
;(0 1 2 3 4 5 6 7 8 9 ...)

(print (frontier #'even 23))
;(0 2 4 6 8 10 12 14 16 18 ...)

(print (frontier (function odd) 10))
;(1 3 5 7 9 11 13 15 17 19)
