[plt-scheme] redefinition of built in function needs to be done twice to be recursive?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Sep 30 10:32:47 EDT 2008

At Tue, 30 Sep 2008 10:20:59 -0400, Matthias Felleisen wrote:
> 
> This sure looks like a bug.

Well, this is the same old problem. The top level is hopeless.

The problem is that the definition of `assq' below is expanded and
compiled before it is evaluated. At the time that it's expanded, `assq'
isn't a defined variable; it's an import from `scheme/base'. So the use
of `assq' is compiled as a reference to the built-in `assq',
independent of top-level definitions.

Then the definition is evaluated, so that `assq' at the top-level
switches its binding from an import to a top-level variable.

The second time you expand/compile the definition of `assq', then the
reference to `assq' is compiled as a reference to a variable, and so it
works as expected.

See also

 http://list.cs.brown.edu/pipermail/plt-scheme/2005-November/010348.html


Matthew

> On Sep 30, 2008, at 9:50 AM, John Lawrence Aspden wrote:
> 
> > Hi, in the process of porting a program with a different definition  
> > of assq,
> > I encountered some behaviour which I'm having trouble understanding:
> >
> > pasted verbatim from interactions window, definitions window  
> > contains (only)
> > #lang scheme, run button freshly pressed:
> >
> > Welcome to DrScheme, version 4.1.0.3 [3m].
> > Language: Module; memory limit: 228 megabytes.
> >> (define (assq a alist) (if (null? alist) #f (if (eq? (caar alist)  
> >> a) alist
> > (assq a (cdr alist)))))
> >> (assq 'b '((a . 1)(b . 2)))
> > (b . 2)
> >> (define (assq a alist) (if (null? alist) #f (if (eq? (caar alist)  
> >> a) alist
> > (assq a (cdr alist)))))
> >> (assq 'b '((a . 1)(b . 2)))
> > ((b . 2))
> >>
> >
> > It seems that the first definition works like let and the second  
> > (identical)
> > definition works like letrec.
> >
> > It doesn't happen if you put the definitions in the definition  
> > window, and
> > it doesn't seem to happen if the function's called my-assq.
> >
> > Can anyone explain what's going on?
> >
> > Cheers, John.
> >
> > -- 
> > Contractor in Cambridge UK -- http://www.aspden.com
> >
> > _________________________________________________
> >   For list-related administrative tasks:
> >   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.