[racket] About set-car! and set-cdr!

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Wed Mar 5 16:15:28 EST 2014

(require compatibility/mlist)
(mlist 1 2 3)

See http://docs.racket-lang.org/compatibility/mlists.html?q=mlist#%28def._%28%28lib._compatibility%2Fmlist..rkt%29._mlist%29%29

2014-03-05 22:06 GMT+01:00 Daniel Carrera <dcarrera at gmail.com>:
>
> On 5 March 2014 19:54, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>>
>>
>> Okay, now see mcons.
>
>
>
> Neat... mcons, mcar, mcdr, mpair, set-mcar!, set-mcdr!
>
> I notice that there is no mlist. Is there a shortcut similar to '(1 2 3) to
> quote a list in a way that makes mutable pairs instead of regular pairs?
>
> Cheers,
> Daniel.
>
>
>>
>>
>> On Mar 5, 2014, at 1:53 PM, Daniel Carrera wrote:
>>
>> I see.
>>
>> k is '(42 2 3) while l is '(1 2 3). This is what I expected to happen, but
>> it is clearly not what was supposed to happen. I just tried the same example
>> with Chicken, and for Chicken both k and l are equal to '(42 2 3).
>>
>> Thanks for the explanation.
>>
>> Cheers,
>> Daniel.
>>
>> On 5 March 2014 19:24, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>>>
>>>
>>> Try
>>>
>>>  (define l (list 1 2 3))
>>>  (define k l)
>>>
>>> Now what does (set-car! k 42) do? What should it do?
>>>
>>>
>>> On Mar 5, 2014, at 1:23 PM, Daniel Carrera wrote:
>>>
>>> But isn't the final effect the same? The pair may be immutable, but I can
>>> make a new pair and bind it to the old variable. The main difference that I
>>> can see is that what I wrote is a macro, while I believe set-car! is
>>> supposed to be a function. That could potentially break code.
>>>
>>> Cheers,
>>> Daniel.
>>>
>>>
>>> On 5 March 2014 19:18, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>>>>
>>>>
>>>> No, set! mutates variable bindings while set-car! mutates cons cells
>>>> (the first slot of a data structure).
>>>>
>>>>
>>>> On Mar 5, 2014, at 1:13 PM, Daniel Carrera wrote:
>>>>
>>>> > Hello,
>>>> >
>>>> > My understanding is that Racket intentionally does not provide
>>>> > set-car! and set-cdr! and that this is one of the ways in which Racket is
>>>> > not fully compatible with Scheme.
>>>> >
>>>> > Am I right to think that it is trivially easy to add these features to
>>>> > Racket? Specifically, I'm thinking of:
>>>> >
>>>> >
>>>> > (define-syntax set-car!
>>>> >   (syntax-rules ()
>>>> >     ((_ l new_car) (set! l (cons new_car (cdr l))))))
>>>> >
>>>> > (define-syntax set-cdr!
>>>> >   (syntax-rules ()
>>>> >     ((_ l new_cdr) (set! l (cons (car l) new_cdr)))))
>>>> >
>>>> >
>>>> > Or did I miss something?
>>>> >
>>>> > Cheers,
>>>> > Daniel.
>>>> > --
>>>> > When an engineer says that something can't be done, it's a code phrase
>>>> > that means it's not fun to do.
>>>> > ____________________
>>>> >  Racket Users list:
>>>> >  http://lists.racket-lang.org/users
>>>>
>>>
>>>
>>>
>>> --
>>> When an engineer says that something can't be done, it's a code phrase
>>> that means it's not fun to do.
>>>
>>>
>>
>>
>>
>> --
>> When an engineer says that something can't be done, it's a code phrase
>> that means it's not fun to do.
>>
>>
>
>
>
> --
> When an engineer says that something can't be done, it's a code phrase that
> means it's not fun to do.
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>



-- 
--
Jens Axel Søgaard


Posted on the users mailing list.