Programming Languages (CSC-302 98S)
Outline of Class 30: Lazy Evaluation, Revisited
Held: Wednesday, April 15, 1998
- Don't forget that
assignment five is
due today. I'll do my best to get it back to you by Friday.
- We won't have another assignment this week since we have an exam
scheduled for next Friday. I'll try to have a review sheet ready
on Monday. [And yes, I'll strive to make the next exam somewhat
shorter.]
- If you're interested in learning a little about the history of computing,
feel free to sit in on CS152 at 8am on Friday and Monday. If you do
plan to sit in, please take my silly survey at
http://www.math.grin.edu/~rebelsky/Courses/CS152/98S/Surveys/history.html
- On Tuesday, April 21 at 11 a.m. in Science 1023, Robert Cadmus,
professor of Physics, will present a lecture about electronic imaging
technology.
- Tomorrow at 4:15, Dick Randell of the University of Iowa will be
giving a Great Math Talk! on Knot Theory. Anyone who's
had linear should be able to understand it (I think), so I encourage
you all to attend. Since I've never taken linear :-), I won't be
there.
- Since I haven't used our Haskell compiler, I won't guarantee that any
of today's I/O examples work correctly. I care mostly that you get
the general idea of how to do I/O, and not necessarily the details.
- Hugs, a Haskell interpreter, is now installed on the Glimmer Mac. Let me
know if you want help using it.
- If you haven't done so already, please start to read the chapter in
Louden on Logic Programming. My Louden is currently missing, but I
believe that it's chapter 11.
Much of today's class was spent on topics that I had not
prepared notes for. In particular, we discussed
- Some issues in the meaning of lazy evaluation (e.g., when is a
value "needed" if it's not needed in order to apply the function).
- How Haskell's pattern-based function definitions relate to those
of a mathematician (Haskell orders them, mathematicians usually
do not; mathematicians tend to prefer independent patterns).
- How one might determine which arguments are "needed" in order to
choose which pattern to apply.
- Like Scheme, Haskell is very nice for working with dynamic data structures,
particularly with lists.
- Haskell, however, provides additional power by allowing you to
recursively define lists (thereby creating infinite lists).
- For example, we can create a list of all integers starting with N with
intsfrom :: Int -> [Int]
intsfrom n = n : (intsfrom (n+1))
|
- Why would we want to do this? Well, it can help with some definitions.
More importantly, it may help us meet some software engineering criteria.
- One of my favorites is "When natural, break a function into component
parts".
- Consider the problem of "find the first N positive integers".
- Part one: find the list of positive integers.
- Part two: extract the first N elements of a list.
- In Haskell,
npos :: Int -> [Int]
npos n = take n ints
ints :: [Int]
ints = intsfrom 1
intsfrom :: Int -> [Int]
intsfrom n = n : (intsfrom (n+1))
take 0 whatever = []
|
- We could also employ a similar technique to find the list of primes,
using the legendary "Sieve of Eratothenes".
- Eratothenes, a great Greek philosopher/mathematician, suggested that
one could identify the primes by
make a list of numbers starting with 2
repeatedly
grab the first remining number. that number is a prime.
sieve out all multiples of that number.
end repeat
|
- For example,
- Start with 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,...
- 2 is a prime. Sieve out multiples.
- Left with with 3,5,7,9,11,13,15,17,19,21,23,25,27,29,...
- 3 is a prime. Sieve out multiples.
- Left with with 5,7,11,13,17,19,23,25,29,...
- 5 is a prime. Sieve out multiples.
- Left with with 7,11,13,17,19,23,29,...
- 7 is a prime. Sieve out multiples.
- Left with 11,13,17,19,23,29,...
- ...
- In many languages, we set up an array of bits to solve the problem.
This allows us to limit the largest prime we can compute, but doesn't
give us a natural way to compute a particular number of primes.
- In Haskell (or any language that supports lazy lists), we can set up
a sequence of filters. We'll try to do this as a group.