Path: utzoo!attcan!uunet!dino!ux1.cso.uiuc.edu!iuvax!purdue!mentor.cc.purdue.edu!ksb From: ksb@mentor.cc.purdue.edu (Kevin Braunsdorf) Newsgroups: comp.lang.misc Subject: Lazy Evaluation... a simple example Summary: an example Keywords: lcons, cons, LISP Message-ID: <7476@mentor.cc.purdue.edu> Date: 14 Feb 90 17:28:34 GMT References: <3109@caesar.cs.montana.edu> <12017@goofy.megatest.UUCP> Reply-To: ksb@mentor.cc.purdue.edu (Kevin Braunsdorf) Organization: Purdue UNIX Group Lines: 32 When I was a student here at Purdue I read a few papers about lazy evaluation and became interested in it. Since I was writing a LISP interp. for a class project I set about to implement a lazy eval feature. What I did was much simpler than most of the systems I saw described at the time. I simply made `cons' defer evaluation of its arguments until either `car' or `cdr' was called on the resulting CONSBOX (which was tagged as being LAZY, and had a saved environment hung on it). Later I made the deferred one be `lcons' and put back the original `cons' because `lcons' is slow if you do not need the power. An example: (defun ilister (n) (lcons n (ilister (+ '1 n)))) This function will produce an infinite list of integers, starting at n. If we had a function `head' (like the UNIX command) (head '10 (ilister '0)) would only compute (about) the first 11 elements in the sequence, and output: (0 1 2 3 4 5 6 7 8 9) If anyone is interested in a silly Pure LISP interp that does this... -- Lightning strikes! Maybe once, maybe twice. Oh, and it lights up the night! Kevin Braunsdorf, ksb@cc.purdue.edu, pur-ee!ksb, purdue!ksb