(apparently) Undeclared variables

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

(apparently) Undeclared variables

ParadoxMachine
This post was updated on .
Hello.

I'm having problems with this code:

a := 0. 
b := 1. 
10 timesRepeat: [
	a timesRepeat: [b = b*2].
	Transcript show: '2^a = '.
	Transcript show: b ;cr.
	a = a+1.
	b = 1].

That code results in the following text being written in Transcript:

2^a = 1

UndefinedObject>>DoIt (a is Undeclared) 
UndefinedObject>>DoIt (b is Undeclared) 
UndefinedObject>>DoIt (a is Undeclared) 
UndefinedObject>>DoIt (b is Undeclared) 
UndefinedObject>>DoIt (b is Undeclared) 
UndefinedObject>>DoIt (b is Undeclared) 
UndefinedObject>>DoIt (a is Undeclared) 
UndefinedObject>>DoIt (a is Undeclared) 
UndefinedObject>>DoIt (b is Undeclared) 

I don't know how to fix this.

Thank you for reading.

Edit: By the way, how do I put two strings together? And how do I convert a number to a string?
Reply | Threaded
Open this post in threaded view
|

Re: Unknown error

Ben Coman
ParadoxMachine wrote:

> Hello.
>
> I'm having problems with this code:
>
>
>
> That code results in the following text being written in Transcript:
>
>
>
> I don't know how to fix this.
>
> Thank you for reading.
>
>
>
> --
> View this message in context: http://forum.world.st/Unknown-error-tp4761926.html
> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>  

I don't see any code or Transcript output.  Were there meant to be images?
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Unknown error

ParadoxMachine
No, they were supposed to appear as raw text (using raw tags). At least they do for me.

Anyway, here's the code:

a := 0.
b := 1.
10 timesRepeat: [
        a timesRepeat: [b = b*2].
        Transcript show: '2^a = '.
        Transcript show: b ;cr.
        a = a+1.
        b = 1].

And this is the result:

2^a = 1

UndefinedObject>>DoIt (a is Undeclared)
UndefinedObject>>DoIt (b is Undeclared)
UndefinedObject>>DoIt (a is Undeclared)
UndefinedObject>>DoIt (b is Undeclared)
UndefinedObject>>DoIt (b is Undeclared)
UndefinedObject>>DoIt (b is Undeclared)
UndefinedObject>>DoIt (a is Undeclared)
UndefinedObject>>DoIt (a is Undeclared)
UndefinedObject>>DoIt (b is Undeclared)
Reply | Threaded
Open this post in threaded view
|

Re: Unknown error

Ben Coman
ParadoxMachine wrote:

> No, they were supposed to appear as raw text (using raw tags). At least they
> do for me.
>
> Anyway, here's the code:
>
> a := 0.
> b := 1.
> 10 timesRepeat: [
> a timesRepeat: [b = b*2].
> Transcript show: '2^a = '.
> Transcript show: b ;cr.
> a = a+1.
> b = 1].
>
> And this is the result:
>
> 2^a = 1
>
> UndefinedObject>>DoIt (a is Undeclared)
> UndefinedObject>>DoIt (b is Undeclared)
> UndefinedObject>>DoIt (a is Undeclared)
> UndefinedObject>>DoIt (b is Undeclared)
> UndefinedObject>>DoIt (b is Undeclared)
> UndefinedObject>>DoIt (b is Undeclared)
> UndefinedObject>>DoIt (a is Undeclared)
> UndefinedObject>>DoIt (a is Undeclared)
> UndefinedObject>>DoIt (b is Undeclared)
>
>
>
> --
> View this message in context: http://forum.world.st/apparently-Undeclared-variables-tp4761926p4761938.html
> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>  
your last two assignments are "=" and not ":=" ?
cheers -ben
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Unknown error

ParadoxMachine
No, that doesn't work, either. The result is exactly the same as the one I mentioned.
Thanks for replying, though.

-ParadoxMachine
Reply | Threaded
Open this post in threaded view
|

Re: Unknown error

Ben Coman
ParadoxMachine wrote:

> No, that doesn't work, either. The result is exactly the same as the one I
> mentioned.
> Thanks for replying, though.
>
> -ParadoxMachine
>
>
>
> --
> View this message in context: http://forum.world.st/apparently-Undeclared-variables-tp4761926p4761943.html
> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>  
That change to your code works for me, but I should have said last three
assignments.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Unknown error

ParadoxMachine
That worked, thanks.

By the way, how do I put strings (or a string and a number) together? The way I did it requires me to write more than a single "Transcript show:" line.

-ParadoxMachine
Reply | Threaded
Open this post in threaded view
|

Re: Unknown error

Ben Coman
ParadoxMachine wrote:
> That worked, thanks.
>
> By the way, how do I put strings (or a string and a number) together? The
> way I did it requires me to write more than a single "Transcript show:"
> line.
>
> -ParadoxMachine
>  
Two alternatives...
* Transcript crShow: 1 asString, String tab, 2 asString.
* Transcript crShow: 1 ; tab ; show: 2.

As well, you may find the links useful...
http://wiki.squeak.org/squeak/5699
http://www.eli.sdsu.edu/courses/spring01/cs635/readingSmalltalk.pdf

btw, just some general feedback on your code. Since b is set to 1 before
the loop and at the end of the loop, it is equivalent to just do that at
the start of the loop instead (unless you are relying on b=1 after the
loop. And here is an alternative...
0 to: 9 do: [     :a |
    b := 1.
    a timesRepeat: [b := b*2].
    Transcript show: '2^a = '.
    Transcript show: b ;cr.
].

cheers, Ben
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Unknown error

ParadoxMachine
Thank you for the answer and the feedback!

-ParadoxMachine