Why didn't variable change? 
Author Message
 Why didn't variable change?

1. Why wasn't the content of my list variable(:m)changed?
2. Is there a better way to shift (move) items in a list?

Example:

make "m [1 2 3 4]
show :m
[1 2 3 4]
shiftfb :m    ;shift first item of list to end of list (front2back)
[1 2 3 4]
[2 3 4 1]
show :m
[1 2 3 4]     ? Why didn't the content of  :m  change to [2 3 4 1]

shiftbf :m    ;shift last item of list to front of list (back2front)
[1 2 3 4]
[4 1 2 3]
show :m
[1 2 3 4]     ? Why didn't the content of  :m  change to [4 1 2 3]
-------------------
to shiftBF :mlist
;shift back to front
 show :mlist
 make "mlist fput (last :mlist)(bl :mlist)
 show :mlist
end

to shiftFB :mlist
;shift front to back
 show :mlist
 queue "mlist (pop "mlist)
 show :mlist
end
------------------
newbe Jay

To unsubscribe from this group, send an email to:

LogoForum messages are archived at:
http://www.*-*-*.com/

Your use of Yahoo! Groups is subject to http://www.*-*-*.com/



Sat, 21 Feb 2004 12:37:41 GMT  
 Why didn't variable change?
The logo function makes a copy of the list. To modify the variable after the
shift, you must do something like:

make "m shiftbf :m

On the other hand, .setfirst and .setbf (mswlogo/ucblogo) *WILL* modify the
actual list.

make "m [1 2 3 4]
.setfirst :m 20
show :m
; gives [20 2 3 4]

Using .setfirst/.setbf is considered dangerous as you can crash a logo
compiler by:
make "m [1 2 3 4]
.setfirst :m :m
show :m

(Don't try this if you have a workspace that needs saving)

For anyone reading this who wants to understand why this is the case
(technical):

Logo stores a list as nodes. The list [1 2 3 4] is represented internally by
4 list nodes.
A list node in Logo has two parts. "first" part and "butfirst" part. (In
it's predecessor LISP, these were called car and cdr, Contents Address
Register, Contents Data Register... talk about bringing implementation to
the surface). I'll represent this as (first,butfirst)
The list [1 2 3 4] is represented as (1,(2,(3,(4,[]))))
The variable "m" points to the{*filter*}node, so points to (1,...)
When you use shiftbf/reverse, a new list is created (2,(3,(4,(1,[])))) but
the variable "m" is still pointing to the list (1,...)
Using ".setfirst" modifies the "first" value. In the example above, I made
the 'first' value of (1,...) reference the list node (1,...). When logo
tries to parse this list, it effectively sees (1,(1,(1,(1,(1,(1,(1,(1,...
infinitely .....

Quote:
-----Original Message-----

Sent: Monday, September 03, 2001 6:44 AM

Subject: [LogoForum] Why didn't variable change?

1. Why wasn't the content of my list variable(:m)changed?
2. Is there a better way to shift (move) items in a list?

Example:

make "m [1 2 3 4]
show :m
[1 2 3 4]
shiftfb :m    ;shift first item of list to end of list (front2back)
[1 2 3 4]
[2 3 4 1]
show :m
[1 2 3 4]     ? Why didn't the content of  :m  change to [2 3 4 1]

shiftbf :m    ;shift last item of list to front of list (back2front)
[1 2 3 4]
[4 1 2 3]
show :m
[1 2 3 4]     ? Why didn't the content of  :m  change to [4 1 2 3]
-------------------
to shiftBF :mlist
;shift back to front
 show :mlist
 make "mlist fput (last :mlist)(bl :mlist)
 show :mlist
end

to shiftFB :mlist
;shift front to back
 show :mlist
 queue "mlist (pop "mlist)
 show :mlist
end
------------------
newbe Jay

To unsubscribe from this group, send an email to:

LogoForum messages are archived at:
http://www.*-*-*.com/

Your use of Yahoo! Groups is subject to http://www.*-*-*.com/

To unsubscribe from this group, send an email to:

LogoForum messages are archived at:
http://www.*-*-*.com/

Your use of Yahoo! Groups is subject to http://www.*-*-*.com/



Sat, 21 Feb 2004 12:40:03 GMT  
 Why didn't variable change?

Quote:

> 1. Why wasn't the content of my list variable(:m)changed?
> 2. Is there a better way to shift (move) items in a list?

What you've run up against here is the concept of 'scope' a dearly
beloved topic here on the Logo list.

Basically logo function calls are by value. That is a COPY of the
variables are sent to the function, not the variables themselves.  This
contrasts with call by reference where a reference (pointer) to the
variables are sent to the function.

So in your example you first create 'm' and set it to the list [1 2 3 4]
(which you proved by doing the 'show :m') then you called your function
shiftbf. Logo sent a COPY of 'm' to shiftbf. Shiftbf did its thing on
'mlist' (the copy of m) which again you proved by doing the show's in
shiftbf. Then shiftbf ended and you return to the top level. Here when
you looked at 'm' it was stilled set to the original list.

Again this is because shiftbf worked on a copy of 'm'.  There are a
number of ways around this. The simplest is to have shiftbf return
(output) 'mlist' and set 'm' to this returned value at the top level.
Like such:

make "m [1 2 3 4]
make "m shiftbf :m

to shiftbf :mlist

.....

output :mlist
end

While what Jamie said it another reply is technically correct it doesn't
apply in this case. Using .setfirst of .setbf wouldn't fix the problem
you are having here.

regards

--
Frank Caggiano                          "The best education for the best

http://www.crystal-objects.com/                Robert M. Hutchins

Logo Users Ring
http://www.crystal-objects.com/logo/logoring.html



Sat, 21 Feb 2004 21:31:27 GMT  
 Why didn't variable change?
Actually, I was showing the .setfirst as comparison rather than suggesting
it as a solution (I had already shown the solution before going off on the
.setfirst tangent) But I think you put this across in a more elegant way :-)

Jamie

Quote:
-----Original Message-----
From: Frank Caggiano
Sent: Tuesday, September 04, 2001 8:42 PM

Subject: Re: [LogoForum] Why didn't variable change?

The message below is being cross-posted from comp.lang.logo.  Please reply



> 1. Why wasn't the content of my list variable(:m)changed?
> 2. Is there a better way to shift (move) items in a list?

What you've run up against here is the concept of 'scope' a dearly
beloved topic here on the Logo list.

Basically logo function calls are by value. That is a COPY of the
variables are sent to the function, not the variables themselves.  This
contrasts with call by reference where a reference (pointer) to the
variables are sent to the function.

So in your example you first create 'm' and set it to the list [1 2 3 4]
(which you proved by doing the 'show :m') then you called your function
shiftbf. Logo sent a COPY of 'm' to shiftbf. Shiftbf did its thing on
'mlist' (the copy of m) which again you proved by doing the show's in
shiftbf. Then shiftbf ended and you return to the top level. Here when
you looked at 'm' it was stilled set to the original list.

Again this is because shiftbf worked on a copy of 'm'.  There are a
number of ways around this. The simplest is to have shiftbf return
(output) 'mlist' and set 'm' to this returned value at the top level.
Like such:

make "m [1 2 3 4]
make "m shiftbf :m

to shiftbf :mlist

....

output :mlist
end

While what Jamie said it another reply is technically correct it doesn't
apply in this case. Using .setfirst of .setbf wouldn't fix the problem
you are having here.

regards

--
Frank Caggiano                          "The best education for the best

http://www.crystal-objects.com/                Robert M. Hutchins

Logo Users Ring
http://www.crystal-objects.com/logo/logoring.html

To unsubscribe from this group, send an email to:

LogoForum messages are archived at:
http://groups.yahoo.com/group/LogoForum

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/

To unsubscribe from this group, send an email to:

LogoForum messages are archived at:
http://groups.yahoo.com/group/LogoForum

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/



Sun, 22 Feb 2004 20:19:35 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Something else for a change / Parallel Computer for APL - in case you didn't know

2. Why didn't PL/I take off?

3. Why I didn't chose LISP

4. tcl8's expr changes: why I think it's bad

5. If dynamic typing didn't exist you'd have to invent it :-)

6. Can't set IDT from C (hope I didn't send this before)

7. (meta) 'Sorry if my cancellations didn't work

8. Can't change variables in a browse window

9. OS/400 REXX - changing variables via API's

10. Why don't my global variables work?

11. Changing a variable's status

12. DLL changes variable it shouldn't

 

 
Powered by phpBB® Forum Software