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

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Sep 30 10:29:42 EDT 2008

On Sep 30, 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? 

While the first expression is compiled, `assq' is a known constant
that gets inlined.  So the definition is not recursive, and you can
get the same by using

  (define (assq a alist) (assq a alist))

When the second expression is compiled, it's (`assq') is no longer the
known constant, so it is not inlined.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.