[Newbies] Modify block (closure) parameters

2013-08-05 Thread psea
Hi Smalltalkers! 

Here is a question I can't find answer with google. 
In short: Does block parameters and block local variables are the same thing
(semantically) inside closure? It looks like it's not. Below the
explanation. 

=== 
Here is the accumulator function (first attempt): 

makeAcc := [ :acc |   
[:n | acc:=acc+n. acc]] 

It turns out it doesn't work as intended: 

a1 := makeAcc value: 10 
a1 value: 1 => 11 
a1 value: 1 => 11 
a1 value: 2 => 12 

 
Here is the second attempt: 

makeAcc := [ :acc |   
| total | 
total:=acc. 
[:n | total:=total+n. total]] 

And it does work as intended: 

a1 := makeAcc value: 10 
a1 value: 1 => 11 
a1 value: 1 => 12 
a1 value: 2 => 14 

So if we use the local variable to store accumulator it works as it should
and remembers the value between function (block) calls. But if we use block
parameter to store the value of accumulator it does not remembers the value
between calls. 
Why is it so? 
 
P.S. Sorry for my English. I do all my best.



--
View this message in context: 
http://forum.world.st/Modify-block-closure-parameters-tp4702118.html
Sent from the Squeak - Beginners mailing list archive at Nabble.com.
___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners


Re: [Newbies] Modify block (closure) parameters

2013-08-05 Thread Casey Ransberger
There's a preference (I can't recall the name but it's something like "allow 
block argument assignment"; I'll try to find it for you when I get back to the 
computer. Anyhow enabling this preference is there for backwards compatibility 
with old code that assigns to block args.

If you're writing new code, it's best to just disable the preference. This way 
an error will be signaled when attempting to assign to block arguments, such 
that you'll start getting used to working around this "limitation."

Closures give you a lot, and you won't ever truly *need* to modify a block arg. 
There's always another way to do it.

I won't try to go to deep into closure semantics, but if you do feel you'd like 
to take a deeper dive on the subject, the best explanation of it I've read 
(which -- perhaps sadly -- doesn't use the term "closure" at all) is in chapter 
3.2 of Structure and Interpretation of Computer Programs. 

The bad news is, that book was written about a language that's a bit off topic 
here (Scheme, the language closures were invented for.)

The good news is, if you want to read it, the whole book is online. Here's a 
link to the chapter I mentioned:

http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-21.html#%_sec_3.2

Hope this helps!

Casey

On Aug 5, 2013, at 1:44 AM, psea  wrote:

> Hi Smalltalkers! 
> 
> Here is a question I can't find answer with google. 
> In short: Does block parameters and block local variables are the same thing
> (semantically) inside closure? It looks like it's not. Below the
> explanation. 
> 
> === 
> Here is the accumulator function (first attempt): 
> 
> makeAcc := [ :acc |   
>[:n | acc:=acc+n. acc]] 
> 
> It turns out it doesn't work as intended: 
> 
> a1 := makeAcc value: 10 
> a1 value: 1 => 11 
> a1 value: 1 => 11 
> a1 value: 2 => 12 
> 
>  
> Here is the second attempt: 
> 
> makeAcc := [ :acc |   
>| total | 
>total:=acc. 
>[:n | total:=total+n. total]] 
> 
> And it does work as intended: 
> 
> a1 := makeAcc value: 10 
> a1 value: 1 => 11 
> a1 value: 1 => 12 
> a1 value: 2 => 14 
> 
> So if we use the local variable to store accumulator it works as it should
> and remembers the value between function (block) calls. But if we use block
> parameter to store the value of accumulator it does not remembers the value
> between calls. 
> Why is it so? 
> 
> P.S. Sorry for my English. I do all my best.
> 
> 
> 
> --
> View this message in context: 
> http://forum.world.st/Modify-block-closure-parameters-tp4702118.html
> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
> ___
> Beginners mailing list
> Beginners@lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
___
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners