[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] redefining cons,car,cdr in SICP

From: Hans Nowak
Subject: Re: [Chicken-users] redefining cons,car,cdr in SICP
Date: Fri, 3 Dec 2010 12:57:25 -0500

On Fri, Dec 3, 2010 at 12:33, David Steiner <address@hidden> wrote:
> i'm reading SICP and practicing in chicken. in the book they redefine
> cons, car and cdr using procedures:
> (define (cons x y)
>  (define (dispatch m)
>    (cond ((= m 0) x)
>          ((= m 1) y)
>          (else (error "Argument not 0 or 1 -- CONS" m))))
>  dispatch)
> (define (car z) (z 0))
> (define (cdr z) (z 1))
> however it produces an error in chicken:
>   Error: (caar) bad argument type: #<procedure (dispatch m)>
> why doesn't it work?

I'm no expert on Chicken's internals (entrails?), but it seems to me
that the problem here is, that you're overwriting the global 'cons'
function, which is probably used by other Chicken functions and
macros. Once you do this, all bets are off: I notice that after
defining just the new 'cons', defining *any* new function produces
this error.

As you already found out, the solution is to use different names that
don't clash with important toplevel functions.

(Out of curiosity, I tried this in Gauche and Gambit, and they seem to
have no problems with it, at first sight. It's just a matter of how
things are implemented in a Scheme system.)

Hans Nowak

reply via email to

[Prev in Thread] Current Thread [Next in Thread]