1. brain gymnastics

Check out this perfectly legal (promise!) Euphoria
 program, what value do you suppose will be printed?

    integer i

    i = 1

    i = -i

    i /= i+1

    +i = +i

    i *= i-1

    -i = -i

    ? i

new topic     » topic index » view message » categorize

2. Re: brain gymnastics

This program sure behaves strangely on my system.
It should give divide by zero error at i /= i+1 but it doesn't - but it will
if you remove the line following.
Huh?

Bye
Martin

----- Original Message -----
From: Tor Gausen <tor.gausen at C2I.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Thursday, June 01, 2000 1:38 AM
Subject: brain gymnastics


> Check out this perfectly legal (promise!) Euphoria
>  program, what value do you suppose will be printed?
>
>     integer i
>
>     i = 1
>
>     i = -i
>
>     i /= i+1
>
>     +i = +i
>
>     i *= i-1
>
>     -i = -i
>
>     ? i
>

new topic     » goto parent     » topic index » view message » categorize

3. Re: brain gymnastics

And it will also give divide by zero error *at the final print i* if you
just insert a print i right after i /= i+1 !?!

Dan Moyer

-----Original Message-----


>This program sure behaves strangely on my system.
>It should give divide by zero error at i /= i+1 but it doesn't - but it
will
>if you remove the line following.
>Huh?
>
>Bye
>Martin
>
>----- Original Message -----
>From: Tor Gausen <tor.gausen at C2I.NET>
>To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
>Sent: Thursday, June 01, 2000 1:38 AM
>Subject: brain gymnastics
>
>
>> Check out this perfectly legal (promise!) Euphoria
>>  program, what value do you suppose will be printed?
>>
>>     integer i
>>
>>     i = 1
>>
>>     i = -i
>>
>>     i /= i+1
>>
>>     +i = +i
>>
>>     i *= i-1
>>
>>     -i = -i
>>
>>     ? i
>>

new topic     » goto parent     » topic index » view message » categorize

4. Re: brain gymnastics

This program is easier to understand if you format it like this:

    integer i
    i = 1
    i = -i
    i /= i+1+(i = +i)  -- this statement originally on 2 lines
    i *= i-1-(i = -i)  -- this statement originally on 2 lines
    ? i

Colin Taylor


----- Original Message -----
From: Tor Gausen <tor.gausen at C2I.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Thursday, June 01, 2000 3:38 AM
Subject: brain gymnastics


> Check out this perfectly legal (promise!) Euphoria
>  program, what value do you suppose will be printed?
>
>     integer i
>
>     i = 1
>
>     i = -i
>
>     i /= i+1
>
>     +i = +i
>
>     i *= i-1
>
>     -i = -i
>
>     ? i
>

new topic     » goto parent     » topic index » view message » categorize

5. Re: brain gymnastics

On Fri, 2 Jun 2000 00:01:53 -0400, Tor Gausen
<tor.gausen at C2I.NET> wrote:

> Check out this perfectly legal (promise!) Euphoria
> program, what value do you suppose will be printed?

>    integer i

>    i = 1

>    i = -i

>    i /= i+1

You should get an error here, at run time - you're dividing by
zero.

>    +i = +i

>    i *= i-1

>    -i = -i

>    ? i


--
Jeff Zeitlin
jzeitlin at cyburban.com

new topic     » goto parent     » topic index » view message » categorize

6. Re: brain gymnastics

Whoops - Jeff is just a little red-faced, having forgotten that a
line break is _not_ a statement separator in Euphoria...
--
Jeff Zeitlin
jzeitlin at cyburban.com

new topic     » goto parent     » topic index » view message » categorize

7. Re: brain gymnastics

> Whoops - Jeff is just a little red-faced, having forgotten that a
> line break is _not_ a statement separator in Euphoria...

>    i /= i+1

You should get an error here, at run time - you're dividing by
zero.

>    +i = +i

Now I understand!
This shouldnt be like this!?

new topic     » goto parent     » topic index » view message » categorize

8. Re: brain gymnastics

> >    integer i
> >    i = 1
> >    i = -i
> >    i /= i+1             [note a]
> >    +i = +i              [note b]
> >    i *= i-1             [note c]
> >    -i = -i
> >    ? i

integer i
i = 1
i = -1
i = i / ( i + 1 + ( i = +i ) )
i = i * ( ( i -1 ) - (i = -i) )
? i

Does precize the same.

notice: a sign is first considered to be part of a calculating (i.e.
statement contiuator: 3 + 4; 5 - 7) secondly part of the number (i.e.
sign: -3; + 4) ..

notice: self reflecting calculations (such as *=, /=, +=, -=) convert to
this form:
[left] [*/+-]= [right]

[left] = [left] [*/+-] (   [right]   )

Everything on the right is used on the calculation.
So:

 i /= i -1
+i = +i

Converts to:
i = i / ( i -1 + (i = +i) )

And will therefor not crash.
More cynically explained: euphoria can be just as messy as C.

Ralf N.

new topic     » goto parent     » topic index » view message » categorize

9. Re: brain gymnastics

On Sun, 4 Jun 2000 15:10:33 +0200, Ralf Nieuwenhuijsen <nieuwen at XS4ALL.NL>
wrote:

>> >    integer i
>> >    i =3D 1
>> >    i =3D -i
>> >    i /=3D i+1             [note a]
>> >    +i =3D +i              [note b]
>> >    i *=3D i-1             [note c]
>> >    -i =3D -i
>> >    ? i
>
>integer i
>i =3D 1
>i =3D -1
>i =3D i / ( i + 1 + ( i =3D +i ) )
>i =3D i * ( ( i -1 ) - (i =3D -i) )
>? i
>
>Does precize the same.

Run the two programs as they stand, Ralf. Yours returns '2'. The first
returns '0'... smile

I made the same mistake right after I'd figured out the linebreak trick...

I've noticed those who have tried to solve Tor's problem have all been
doing the same thing:

There's a tendency to see (things like):
    a *=3D a + b + c =3D d
      -- Uh-oh.

...as:
    a =3D a * (a + b + (c =3D d))
      -- Multiply 'a' by (a + b) if c !=3D d, and (a + b + 1) if c =3D d.

...when we should be reading it as:
    a =3D a * ((a + b + c) =3D d)
      -- Make 'a' zero if (a + b + c) =3D d, leave it alone otherwise.

Yet in situations like this:
    if a + b + c =3D d then...

...we know exactly what the expression means!

I reckon that this is some kind of "codical" illusion, and also a darn good
argument for using parentheses to make expressions more readable. smile

Carl

--
Insert clich=E9 about inserting witty comment here, here.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu