help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] scripting error ?


From: Paolo Bonzini
Subject: Re: [Help-smalltalk] scripting error ?
Date: Tue, 09 Dec 2008 16:39:46 +0100
User-agent: Thunderbird 2.0.0.18 (Macintosh/20081105)

Mehul N. Sanghvi wrote:
> OK so I've started scripting and can do the simple
> 
>    'Hello' printNl . !
> 
> type of script.
> 
> I have the following:
> 
>     #! /bin/sh
>     "exec" "gst" "-f" "$0" "$@"
> 
>     'Project Euler:  Problem 1: ' print .
> 
>     | sum i limit |
>     sum := 0 .
>     i := 0 .
>     limit := 10 .
> 
>     [ i := i + 1.
>       i < limit ifTrue: [ ((( i \\ 3) = 0) | ((i \\ 5) = 0)) ifTrue: [
> sum := sum + i ] ].
>     ] repeat.
> 
>     !
> 
> 
> This will do the print, and then just wait there.  All I want to do is a
> simple while-loop type thing where the code loops until counter reaches
> 1000.  Here's the equivalent Perl code:

Repeat is an infinite loop.  What you want is

[ i := i + 1. i < limit ] whileTrue: [
  ((( i \\ 3) = 0) | ((i \\ 5) = 0)) ifTrue: [ sum := sum + i ] ].
sum printNl.

or better:

[ i := i + 1. i < limit ] whileTrue: [
  (( i \\ 3) = 0 or: [(i \\ 5) = 0]) ifTrue: [ sum := sum + i ] ].
sum printNl.

or also

1 to: limit do: [ :i |
  (( i \\ 3) = 0 or: [(i \\ 5) = 0]) ifTrue: [ sum := sum + i ] ].
sum printNl.

(Pardon me for any typos, I haven't tried the above).

Paolo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]