Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   NZ Computing (http://www.velocityreviews.com/forums/f47-nz-computing.html)
-   -   What Will This Print? (http://www.velocityreviews.com/forums/t704613-what-will-this-print.html)

 Lawrence D'Oliveiro 11-08-2009 12:30 PM

What Will This Print?

What output will this Algol 68 produce?

PROC VOID proc1 = BEGIN print(("line 1", new line)); GOTO end END;
print(("line 2", new line));
proc1;
print(("line 3", new line));
end: SKIP

 Nik Coughlin 11-08-2009 11:22 PM

Re: What Will This Print?

"Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in message
news:hd6dkj\$e6r\$1@lust.ihug.co.nz...
> What output will this Algol 68 produce?
>
> PROC VOID proc1 = BEGIN print(("line 1", new line)); GOTO end END;
> print(("line 2", new line));
> proc1;
> print(("line 3", new line));
> end: SKIP

line 2
line 1

 Lawrence D'Oliveiro 11-09-2009 12:00 AM

Re: What Will This Print?

In message <hd7jqo\$98a\$1@news.eternal-september.org>, Nik Coughlin wrote:

> "Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in message
> news:hd6dkj\$e6r\$1@lust.ihug.co.nz...
>
>> What output will this Algol 68 produce?
>>
>> PROC VOID proc1 = BEGIN print(("line 1", new line)); GOTO end END;
>> print(("line 2", new line));
>> proc1;
>> print(("line 3", new line));
>> end: SKIP

>
> line 2
> line 1

Actually it will print

line 1
line 2

Why?

 Nik Coughlin 11-09-2009 12:09 AM

Re: What Will This Print?

"Nik Coughlin" <nrkn.com@gmail.com> wrote in message
news:hd7jqo\$98a\$1@news.eternal-september.org...
> "Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in message
> news:hd6dkj\$e6r\$1@lust.ihug.co.nz...
>> What output will this Algol 68 produce?
>>
>> PROC VOID proc1 = BEGIN print(("line 1", new line)); GOTO end END;
>> print(("line 2", new line));
>> proc1;
>> print(("line 3", new line));
>> end: SKIP

>
> line 2
> line 1

Nope.

line 1
line 2

Algol is quite different to what I'm used to. I'd assumed that the above
was equivalent to (in c-ish pseudocode):

void proc1() {
print( "line1\n" );
goto end;
}

print( "line2\n" );
proc1;
print( "line3\n" );
end: //do nothing

 Nik Coughlin 11-09-2009 12:14 AM

Re: What Will This Print?

"Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in message
news:hd7m2v\$5il\$2@lust.ihug.co.nz...
> In message <hd7jqo\$98a\$1@news.eternal-september.org>, Nik Coughlin wrote:
>
>> "Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in message
>> news:hd6dkj\$e6r\$1@lust.ihug.co.nz...
>>
>>> What output will this Algol 68 produce?
>>>
>>> PROC VOID proc1 = BEGIN print(("line 1", new line)); GOTO end END;
>>> print(("line 2", new line));
>>> proc1;
>>> print(("line 3", new line));
>>> end: SKIP

>>
>> line 2
>> line 1

>
> Actually it will print
>
> line 1
> line 2
>
> Why?

It looks like what it's doing is executing the first part of the proc when
it's defined, and then executing the GOTO end (which "skips" the "line 3")
when it's called?

So the program flow goes something like this:

proc1 is defined, print "line 1"
print "line 2"
proc1 gets called, goes to the end label which tells it to skip the next
statement (the print "line 3")
done

 Nik Coughlin 11-09-2009 12:14 AM

Re: What Will This Print?

"Nik Coughlin" <nrkn.com@gmail.com> wrote in message
news:hd7msj\$57k\$1@news.eternal-september.org...
> "Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in message
> news:hd7m2v\$5il\$2@lust.ihug.co.nz...
>> In message <hd7jqo\$98a\$1@news.eternal-september.org>, Nik Coughlin wrote:
>>
>>> "Lawrence D'Oliveiro" <ldo@geek-central.gen.new_zealand> wrote in
>>> message
>>> news:hd6dkj\$e6r\$1@lust.ihug.co.nz...
>>>
>>>> What output will this Algol 68 produce?
>>>>
>>>> PROC VOID proc1 = BEGIN print(("line 1", new line)); GOTO end END;
>>>> print(("line 2", new line));
>>>> proc1;
>>>> print(("line 3", new line));
>>>> end: SKIP
>>>
>>> line 2
>>> line 1

>>
>> Actually it will print
>>
>> line 1
>> line 2
>>
>> Why?

>
> It looks like what it's doing is executing the first part of the proc when
> it's defined, and then executing the GOTO end (which "skips" the "line 3")
> when it's called?
>
> So the program flow goes something like this:
>
> proc1 is defined, print "line 1"
> print "line 2"
> proc1 gets called, goes to the end label which tells it to skip the next
> statement (the print "line 3")
> done

Why does it do that?

 Lawrence D'Oliveiro 11-09-2009 06:11 AM

Re: What Will This Print?

In message <hd7msj\$57k\$1@news.eternal-september.org>, Nik Coughlin wrote:

> It looks like what it's doing is executing the first part of the proc when
> it's defined, and then executing the GOTO end (which "skips" the "line 3")
> when it's called?

The subtle point is that in

PROC VOID proc1 = BEGIN print(("line 1", new line)); GOTO end END;

that "BEGIN ... END" clause is not the procedure body. It's just a clause
that's evaluated and is supposed to yield something that's assigned to
proc1. This should be the result from the last expression in the clause.
However, in this case, that last expression happens to be a GOTO statement.

So something funny happens: instead of being executed, the GOTO statement is
turned into the body of a procedure, and that's what's returned to become
the value of proc1.

In short, the above is more equivalent to

print(("line 1", new line));
PROC proc1 = VOID: GOTO end;

In order to for it to mean what you originally thought it meant, it would
have to be written as

PROC proc1 = VOID: BEGIN print(("line 1", new line)); GOTO end END;

 Lawrence D'Oliveiro 11-09-2009 06:11 AM

Re: What Will This Print?

In message <hd7mjh\$tdi\$1@news.eternal-september.org>, Nik Coughlin wrote:

> Algol is quite different to what I'm used to.

It was designed by computer scientists. Need I say more? :)

 Sailor Sam 11-09-2009 06:22 AM

Re: What Will This Print?

Lawrence D'Oliveiro wrote:
> In message <hd7mjh\$tdi\$1@news.eternal-september.org>, Nik Coughlin wrote:
>
>> Algol is quite different to what I'm used to.

>
> It was designed by computer scientists. Need I say more? :)

As was B.

 Ted 11-09-2009 08:22 AM

Re: What Will This Print?

On Nov 9, 7:11*pm, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealand> wrote:
> In message <hd7mjh\$td...@news.eternal-september.org>, Nik Coughlin wrote:
>
> > Algol is quite different to what I'm used to.

>
> It was designed by computer scientists. Need I say more? :)

This is what some computer scientists had to say about it at the time:

* The more I see of it, the more unhappy I become - E.W. Dijkstra,
1968 [23]

* [...] it was said that A68's popularity was inversely
proportional to [...] the distance from Amsterdam - Guido van Rossum
[24]

* 1980 quote: '[...] The best we could do was to send with it a
minority report, stating our considered view that, "... as a tool for
the creation of sophisticated programs, the language was a
failure." [...] ' - C. A. R. Hoare, Oct 1980, re: "Dec 1968"

* Original 1968 version: "[...] More than ever it will be required
from an adequate programming tool that it assists, by structure, the
programmer in the most difficult aspects of his job, viz. in the
reliable creation of sophisticated programs. In this respect we fail
to see how the language proposed here [Algol68] is a significant step
forward: on the contrary, we feel that its implicit view of the
programmer's task is very much the same as, say, ten years ago. This
forces upon us the conclusion that, regarded as a programming tool,
the language must be regarded as obsolete. [...]" Signed by: DIJKSTRA,
DUNCAN, GARWICK, HOARE, RANDELL, SEEGMUELLER, TURSKI, WOODGER. And
then on Dec. 23, 1968, Jan V. Garwick[25]

http://en.wikipedia.org/wiki/ALGOL_68

All times are GMT. The time now is 03:43 PM.