[plt-scheme] redefinition with #lang scheme

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Fri Apr 2 12:06:08 EDT 2010

2010/4/2 Carl Eastlund <cce at ccs.neu.edu>:
> 2010/4/2 Skeptic . <skeptic2000 at hotmail.com>:
>>
>> Hi,
>> I know that the following code works on the REPL, but why/how it should be
>> done to work with #lang scheme ?
>> (define old-car car)
>> (define car
>>  (λ (lst)
>>   (if (null? lst)
>>   '()
>>   (old-car lst))))
>> (car '())
>> (car '(a b  c))
>> Thanks.
>
> It doesn't work as you've written it, and this is intentional.
> Changing the behavior of functions used ubiquitously throughout the
> PLT Scheme libraries may break lots of existing code in unpredictable
> ways.  Changes like this are not necessarily compositional, either: if
> two different libraries try to change the behavior of car on null,
> only one will ultimately have effect.
>
> Instead of changing the way the existing "car" works, the way of
> changing car in PLT Scheme is to make a module that provides a new
> language with the new "car":
>
> #lang scheme
> (define (my-car x)
>  (if (null? x) '() (car x)))
> (provide
>  (except-out (all-from-out scheme))
>  (rename-out [my-car car]))

Oops.  Make that last part:

(provide
  (except-out (all-from-out scheme) car) ;; <-- forgot the "car" last time
  (rename-out [my-car car])

> Then save that as "my-lang.ss" write your new programs in your new language:
>
> #lang s-exp "my-lang.ss"
> (car '()) ;; should produce '()
>
> Now you have a language like Scheme, but with your favorite behavior
> for "car", and existing libraries will continue to use the "car" they
> know and love, too.
>
> --Carl


Posted on the users mailing list.