;;; This is one of the example programs from the textbook:
;;; Artificial Intelligence: 
;;; Structures and strategies for complex problem solving
;;; by George F. Luger and William A. Stubblefield
;;; These programs are copyrighted by Benjamin/Cummings Publishers.
;;; We offer them for use, free of charge, for educational purposes only.
;;; Disclaimer: These programs are provided with no warranty whatsoever as to
;;; their correctness, reliability, or any other property.  We have written 
;;; them for specific educational purposes, and have made no effort
;;; to produce commercial quality computer programs.  Please do not expect 
;;; more of them then we have intended.

;;; These functions implement the basic stream handling operations 
;;; with delayed evaluation, as used in chapter 14.

;;; force and delay allow us to control evaluation of expressions.

(defmacro delay (exp) `(function (lambda () ,exp)))

(defun force (function-closure) (funcall function-closure))

;;; Cons-stream adds a new first element to a stream
(defmacro cons-stream (exp stream) 
   `(cons ,exp (delay ,stream)))

;;; Head-stream returns the first element of the stream
(defun head-stream (stream) 
   (car stream))

;;; Tail-stream returns the stream with its first element deleted.
(defun tail-stream (stream) 
   (force (cdr stream)))

;;; Empty-stream-p is true if the stream is empty.
(defun empty-stream-p (stream) 
   (null stream))

;;; Make-empty-stream creates an  empty stream.
(defun make-empty-stream () 

;;; Combine-streams appends two streams.
(defun combine-streams (stream1 stream2) 
   (cond ((empty-stream-p stream1) stream2)
         (t (cons-stream (head-stream stream1)
                         (combine-streams (tail-stream stream1) stream2)))))

;;; Filter-stream
(defun filter-stream (stream test)
   (cond ((empty-stream-p stream) (make-empty-stream))
             ((funcall test (head-stream stream)) 
                      (cons-stream (head-stream stream) 
                                   (filter-stream (tail-stream stream)test)))
             (t (filter-stream (tail-stream stream)test))))

;;; map stream

(defun map-stream (stream func)
   (cond ((empty-stream-p stream) (make-empty-stream))
             (t (cons-stream (funcall func (head-stream stream))
                                    (map-stream (tail-stream stream) func)))))


Close Window