; Can you use P and Q to build a stream?
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 9
; Exercise 10
; 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 (n)
  (+ 1 n))

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

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

(set 'int-maker (str-maker (function add1) 0))

(defun first$ (l) 
  (first l))

(defun second$ (l) 
  (funcall (second l)))

(defun /_(m n)
  (cond
   ((zero m) 0)
   ((< m 0) 0)
   ((< m n) 0)
   (t (+ 1 (/_ (- m n) n)))))


(defun remainder (n m)
  (cond
   (t (- n (* m (/_ n m))))))

(defun Q (str n)
  (cond
   ((zero (remainder (first$ str) n))
         (Q (second$ str) n))
   (t (build (first$ str) (lambda () (Q (second$ str) n))))))

(defun P (str)
  (build (first$ str) (lambda () (P (Q str (first$ str))))))

(defun zero (n)
  (= 0 n))

(defun sub1 (n)
  (- n 1))

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

(print (frontier (P (second$ (second$ int-maker))) 10))
; (2 3 5 7 11 13 17 19 23 29)
; This is a stream of prime numbers

