On Wed, Jan 23, 2008 at 10:16:53PM -0500, Mark Schoonover wrote:

Ex. 1.5

I would think applicative order would return zero since it's going to
evaluate x first. Normal order, I'm not sure what it'll do. Trying it
with drscheme, it just keeps running forever without any end. Consumes
about 50% CPU, and eventually kicks the fan on the laptop CPU. When I
stop the process, it always stoped on (p) in the define.

Here's a little bit of a hint.  Haskell, uses Normal order evaluation,
roughly.  The equivalent program in haskell (same program, just different
notation).

    p = p

    test x y =   if (x == 0) then 0 else y

And then I type the test expression into the interpreter

    --> test 0 p
    0
    -->

It gives me a zero, whereas Scheme is going to hang, trying to "figure out
p".

------------------------------------------------------
--- Stop reading if you don't want more hints, yet ---
------------------------------------------------------

Think of it as substitution.  Your test expression is:

  (test 0 (p))

So, the expansion rule is to expand each argument first.  Test expands out
to the procedure we write (using a notation that hasn't been covered, yet).
The 0 is already expanded, and what does the call to (p) expand to?  The
definition of p is (p).  This is still a function call, and needs to be
expanded.

In haskell, the call to 'p' evalues to p.  If I try to evaluate 'p' in
haskell, the system hangs.  However, since the evaluation doesn't happen
until values are needed, when x is zero, it never uses y, so it doesn't
matter that y wouldn't terminate if it tried to evaluate it.

Challenge question: there is a fairly easy way to simulate normal-order
evaluation in scheme, any idea what it might be?

David

--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to