Loop problem on Dr Stef

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

Loop problem on Dr Stef

Roelof
Hello,

When I select this part:

1 to: 100 do:
   [:i | Transcript show: i asString; cr ].

And do print it.

I only see 1 where I expected to see all the numbers from 1 till 100.

What went wrong ?

Roelof


Reply | Threaded
Open this post in threaded view
|

Re: Loop problem on Dr Stef

Mark Rizun
Hello,

You should open Transcript, and than do it, instead of print it.

Mark


2014-03-30 14:45 GMT+03:00 Roelof Wobben <[hidden email]>:
Hello,

When I select this part:

1 to: 100 do:
  [:i | Transcript show: i asString; cr ].

And do print it.

I only see 1 where I expected to see all the numbers from 1 till 100.

What went wrong ?

Roelof



Reply | Threaded
Open this post in threaded view
|

Re: Loop problem on Dr Stef

Mark Rizun
Or you may even print it, and the result (numbers from 1 to 100) will appear in Transcript window.


2014-03-30 14:48 GMT+03:00 Mark Rizun <[hidden email]>:
Hello,

You should open Transcript, and than do it, instead of print it.

Mark


2014-03-30 14:45 GMT+03:00 Roelof Wobben <[hidden email]>:

Hello,

When I select this part:

1 to: 100 do:
  [:i | Transcript show: i asString; cr ].

And do print it.

I only see 1 where I expected to see all the numbers from 1 till 100.

What went wrong ?

Roelof




Reply | Threaded
Open this post in threaded view
|

Re: Loop problem on Dr Stef

Roelof
In reply to this post by Mark Rizun
Thanks,

I have finished this tutorial and will go on with the first book.

I wonder if there are some kind of challenges so I can pratice more with Smalltalk.

Roelof



Mark Rizun schreef op 30-3-2014 13:48:
Hello,

You should open Transcript, and than do it, instead of print it.

Mark


2014-03-30 14:45 GMT+03:00 Roelof Wobben <[hidden email]>:
Hello,

When I select this part:

1 to: 100 do:
  [:i | Transcript show: i asString; cr ].

And do print it.

I only see 1 where I expected to see all the numbers from 1 till 100.

What went wrong ?

Roelof




Reply | Threaded
Open this post in threaded view
|

Re: Loop problem on Dr Stef

Henrik Sperre Johansen
In reply to this post by Roelof

On 30 Mar 2014, at 1:45 , Roelof Wobben <[hidden email]> wrote:

> Hello,
>
> When I select this part:
>
> 1 to: 100 do:
>  [:i | Transcript show: i asString; cr ].
>
> And do print it.
>
> I only see 1 where I expected to see all the numbers from 1 till 100.
>
> What went wrong ?
>
> Roelof
>
>
"print it" prints the return value of the expression.
In the above case, that is the return value of the to:do: method, which is the receiver, so 1 is printed.
(If you read the implementation of to:do: on Number, you’ll see there is no explicit return using ^ , in such cases the return is always the receiver)

If you wanted to print a list of 1 .. 100 (which would be printed with print it), you’d use a method which returns such a collection for example collect:;
(1 to: 100) collect: [:each | each ].

The () are needed, since there is no to:collect: method, so instead we send  collect: to an interval, which we create using 1 to: 100.

Cheers,
Henry

signature.asc (859 bytes) Download Attachment