Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

(Scheme) set! vs set-car!/set-cdr!

Name: Anonymous 2008-01-25 12:02

Hello there Scheme magicians, I walked across the desert from lands far away to seek your wisdom.
So, I have a list value some-list set to () and I need to change it to, say, (0 1 2 3).  I can't use set! for obvious reasons (set! is like SETQ, a special form and not a first-class procedure) and I can't use set-car!/set-cdr! either because they want a mutable pair.  How do I assigned to a list variable, /prog/?

Name: Anonymous 2008-01-25 12:04

wat

Name: Anonymous 2008-01-25 12:05

(set! some-list '(0 1 2 3))

What's wrong with this?

Name: Anonymous 2008-01-25 12:10

i dont think >>1 know's about '

Name: Anonymous 2008-01-25 12:16

>>2-4
(define my-set!
  (lambda (x y)
    (set! x y)))

(define dong (list 6 7 8))    ; dong is (6 7 8)
(my-set! dong (list 0 1 2 3)) ; after this dong is still (6 7 8)

And if I change set! to set-cdr! I get (6 0 1 2 3), but I want to be able to change lists with only 1 assignment procedure which also could handle ().  Is it possible?//

Name: Anonymous 2008-01-25 12:18

I don't think >>4 knows about '.

Name: Anonymous 2008-01-25 12:41

>>5
Read SICP

Name: Anonymous 2008-01-25 12:50

Why do you want to change this variable's binding?

Name: Anonymous 2008-01-25 12:52

>>5
Try using set! instead of my-set!. This should work.

Name: Anonymous 2008-01-25 13:00

box, unbox and set-box! is what you want

Name: Anonymous 2008-01-25 13:10

>>8-9
I want to change a list value associated with some symbol, not change any bindings, and due to Scheme's lexical scoping I have to pass it as a parameter, and set! changes only nearest instance of my variable, the external one remains the same.
I just want to mutate null and non-null lists with some procedure.

Name: Anonymous 2008-01-25 13:23

>>11
Yes, you're looking to bind the variable to a different value. You're not even attempting to mutate a list in this example. You're binding that variable to the start of a completely different chain of pairs. The question is, why do you even want to do this? What makes (lol-side-effects list-to-be-changed) better than (set! list-to-be-changed (function-returning-a-new-value-for-the-previous-variable-to-be-bound-to))?

Name: Anonymous 2008-01-25 13:33

>>10
I found
(define box (lambda (val) (cons 'box val)))
(define unbox cdr)
(define set-box! set-cdr!)
(define box? (lambda (thing) (and (pair? thing) (eqv? (car thing) 'box))))


There should be a better way to handle mutating a variable holding () :<

>>12
That's exactly right, I was just illustrating that set! does not and should not work in this case.

Name: Anonymous 2008-01-25 14:43

>>1
Your code is shit. Paste it; we need to fix it.

Name: Anonymous 2008-01-25 18:38

DON'T HELP HIM

Name: Anonymous 2008-01-25 18:52

>>1
Forget it, it's NP-Complete

Name: Anonymous 2008-01-25 20:05

>>1
what you want to do is something *rougly* equivalent to this pseudo C code (sorry for the mistakes but I haven't written C code since ages):

typedef struct { int car; Pair * cdr; } Pair;
Pair * Cons(int a, Pair* r)
{
     Pair* ret;
     ret = malloc(sizeof(Pair));
     ret->car = a;
     ret->cdr = r;
     return ret;
}
void yourfun(Pair** p)
{
     p* = Cons(1,Cons(2,Cons(3,null)));
}

void callexample()
{
     Pair* p;
     p = null;
     yourfun(&p);
}

Ok, now. First thing to watch. This function, yourfun, receives a pointer to pointer of Pair. You cannot pass it a pointer to Pair, because then you would need to change what p references to, not only the contents of the destiny. You cannot do this in Lisp, since you can't get the pointer of a variable (nor in Java, Python, Smalltalk, or any language with lisplike semantics of variables - unless java has some kind of "out" parameters like C# which I don't remember it having).

So how do you work this out, not only in lisp, but also in java, smalltalk, and all those languages? you need to "box" it like
>>13 did. then, the box works like a pointer to pointer. now your variable points to a structure, which cointains a pointer to the list you actually want.

That said, that is extremely bad lisp style.

Name: Anonymous 2008-01-25 21:48

If you're asking to change the contents of a list in-place, you're doing it wrong. Use set!, don't wrap it in a procedure. Problem solved.

Name: Anonymous 2008-01-25 21:53

no, he's trying to do something like this, I believe

(let ((l '())) (yourfun l) (assert (eq? l '(1 2 3))))

of course this cannot be done.

Name: Anonymous 2008-01-27 20:02

>>19
it can actually be done with set-cdr! if l was not empty, as noted above, but they work with pairs and not lists so they won't work with () which is a list but not a pair.

Welcome to MzScheme v371 [3m], Copyright (c) 2004-2007 PLT Scheme Inc.
(list? '())
#t
(pair? '())
#f

Name: Anonymous 2008-01-27 21:25

I hope you guys are prepared for set-c*r! to be removed.

Name: Anonymous 2008-01-27 22:10

>>20
(list? '())
(pair? '())
???
What kind of idiot would adopt this naming convention?

Name: Anonymous 2008-01-27 22:36

>>21
My other car is a mcar.

Name: Anonymous 2008-01-28 15:44

>>22
sussman hates you, fag

Name: Anonymous 2008-01-30 1:39

(defmacro mutator (x) `(real-mutator (cons 'cock ,x)))

Name: Anonymous 2009-03-18 3:44

I'm feeling really keen, for some of that good ol' green

Marijuana MUST be legalized.

Name: Anonymous 2010-07-27 4:43

This is my old thread and I feel like I knew more about programming two years ago than I do now.

;_;

Name: Anonymous 2010-07-27 4:47

SETF is where it's at.
I'm not aware of Scheme having generalized places like Common Lisp does, but it shouldn't be hard to implement them portably.

Name: Anonymous 2010-07-27 7:44

>>27 Thank you for bumping it. I enjoyed it.

Name: Anonymous 2010-07-27 9:28

>>27
Have you stopped using set!?

Name: Anonymous 2010-07-28 16:34

the exclamation point is supposed to make you cringe everytime you use it

Name: Anonymous 2010-07-28 16:50

>>31
It's to add emphasis, and so that the sompiler knows you really mean it.

Name: Anonymous 2010-11-16 3:51

Name: Anonymous 2010-12-26 15:43

Don't change these.
Name: Email:
Entire Thread Thread List