Why does this work - haskell mysteries?

Derek Elkins ddarius at hotpop.com
Sat Oct 4 10:53:13 EDT 2003


On Sun, 5 Oct 2003 11:02:37 +0000
Petter Egesund <pegesund at online.no> wrote:

> Hi & thanks for answering.
> 
> I think I got it - the chaning of the functions lies in the last part
> of
> 
>  (\w -> if v==w then n else sto w)
> 
> I am used to higher ordered functions from Scheme, but it was the
> delayed evaluation which played me the trick here. This function is
> built when updating, and not executed before asking value 'x'?!

If by delayed evaluation you mean lazy evaluation then that has nothing
to do with it.  Obviously the function isn't executed before asking the
value of 'x' because no function can run without it's argument(s). The
same representation will behave exactly the same in Scheme.

(define init-store (lambda (key) 0))
(define (lookup-store store key) (store key))
(define (update-store store key value)
   (lambda (lookup-key)
      (if (equal? key lookup-key)
          value
          (lookup-store store lookup-key))))

Obviously, this will make the function taking lookup-key when
update-store is called (just like Haskell) and (just like Haskell) it
will only be executed when applied to a key to lookup.



More information about the Haskell-Cafe mailing list