help-smalltalk
[Top][All Lists]
Advanced

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

Re: [Help-smalltalk] scripting error ?


From: Mehul N. Sanghvi
Subject: Re: [Help-smalltalk] scripting error ?
Date: Tue, 09 Dec 2008 13:07:23 -0500
User-agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018)

Paolo Bonzini said the following on 12/09/2008 10:39 AM:
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 was under the impression that 'repeat' would repeat the previous block until the conditions didn't apply. I guess I'll have to read the 'repeat' doc again.

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


This is what I had at one point in time, except that I was doing the counter increment afterward, which would cause it to just hang and not do anything.

or better:

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


I am sure this is a syntax error on my part, but I got the message:

         Object: false error: did not understand #or:ifTrue:

when trying it like above.

or also

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


This was the original I had, but that always gives the wrong answer because that loop will include the 'limit' as well, whereas what I want is for it to stop if it equal to limit or greater.

The whileTrue: above worked and did what I wanted it to. I'll try playing around the or: syntax to see if I can get that work. Would be nice to do it that way.


cheers,

    mehul

p.s. I'm trying to learn Smalltalk by doing the problems at http://projecteuler.net/





reply via email to

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