What Will This Print?

Discussion in 'NZ Computing' started by Lawrence D'Oliveiro, Nov 8, 2009.

  1. 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
     
    Lawrence D'Oliveiro, Nov 8, 2009
    #1
    1. Advertising

  2. Lawrence D'Oliveiro

    Nik Coughlin Guest

    "Lawrence D'Oliveiro" <_zealand> wrote in message
    news:hd6dkj$e6r$...
    > 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
     
    Nik Coughlin, Nov 8, 2009
    #2
    1. Advertising

  3. In message <hd7jqo$98a$-september.org>, Nik Coughlin wrote:

    > "Lawrence D'Oliveiro" <_zealand> wrote in message
    > news:hd6dkj$e6r$...
    >
    >> 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?
     
    Lawrence D'Oliveiro, Nov 9, 2009
    #3
  4. Lawrence D'Oliveiro

    Nik Coughlin Guest

    "Nik Coughlin" <> wrote in message
    news:hd7jqo$98a$-september.org...
    > "Lawrence D'Oliveiro" <_zealand> wrote in message
    > news:hd6dkj$e6r$...
    >> 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, Nov 9, 2009
    #4
  5. Lawrence D'Oliveiro

    Nik Coughlin Guest

    "Lawrence D'Oliveiro" <_zealand> wrote in message
    news:hd7m2v$5il$...
    > In message <hd7jqo$98a$-september.org>, Nik Coughlin wrote:
    >
    >> "Lawrence D'Oliveiro" <_zealand> wrote in message
    >> news:hd6dkj$e6r$...
    >>
    >>> 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, Nov 9, 2009
    #5
  6. Lawrence D'Oliveiro

    Nik Coughlin Guest

    "Nik Coughlin" <> wrote in message
    news:hd7msj$57k$-september.org...
    > "Lawrence D'Oliveiro" <_zealand> wrote in message
    > news:hd7m2v$5il$...
    >> In message <hd7jqo$98a$-september.org>, Nik Coughlin wrote:
    >>
    >>> "Lawrence D'Oliveiro" <_zealand> wrote in
    >>> message
    >>> news:hd6dkj$e6r$...
    >>>
    >>>> 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?
     
    Nik Coughlin, Nov 9, 2009
    #6
  7. In message <hd7msj$57k$-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, Nov 9, 2009
    #7
  8. In message <hd7mjh$tdi$-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? :)
     
    Lawrence D'Oliveiro, Nov 9, 2009
    #8
  9. Lawrence D'Oliveiro

    Sailor Sam Guest

    Lawrence D'Oliveiro wrote:
    > In message <hd7mjh$tdi$-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.
     
    Sailor Sam, Nov 9, 2009
    #9
  10. Lawrence D'Oliveiro

    Ted Guest

    On Nov 9, 7:11 pm, Lawrence D'Oliveiro <l...@geek-
    central.gen.new_zealand> wrote:
    > In message <hd7mjh$-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
     
    Ted, Nov 9, 2009
    #10
  11. Lawrence D'Oliveiro

    John Little Guest

    On Nov 9, 7:11 pm, Lawrence D'Oliveiro <l...@geek-
    central.gen.new_zealand> wrote:
    ....
    > However, in this case, that last expression happens to be a GOTO statement.
    > So something funny happens...


    In short, "goto" is evil.

    Regards, John
     
    John Little, Nov 10, 2009
    #11
  12. In message <11605cf6-c05f-48fe-
    >, John Little wrote:

    > On Nov 9, 7:11 pm, Lawrence D'Oliveiro <_zealand>

    wrote:
    > ...
    >> However, in this case, that last expression happens to be a GOTO
    >> statement. So something funny happens...

    >
    > In short, "goto" is evil.


    Yeah, but do you realize _how_ evil?

    In Algol 68, the word "GOTO" (or "GO TO") is optional. Instead of

    GOTO label

    you can just write

    label

    Now, how is the compiler supposed to know whether this is a procedure call
    or a goto, if it hasn't seen label before? Answer: it doesn't. It has to
    work it out afterwards.

    Also nonlocal gotos were a common technique for aborting the flow of control
    before exceptions were invented.
     
    Lawrence D'Oliveiro, Nov 10, 2009
    #12
  13. Lawrence D'Oliveiro

    Sailor Sam Guest

    geoff wrote:
    > John Little wrote:
    >> On Nov 9, 7:11 pm, Lawrence D'Oliveiro <l...@geek-
    >> central.gen.new_zealand> wrote:
    >> ...
    >>> However, in this case, that last expression happens to be a GOTO
    >>> statement. So something funny happens...

    >> In short, "goto" is evil.
    >>
    >> Regards, John

    >
    > 10 Print "Dork"
    > 20 Goto 10
    >
    > What does this print ?
    >
    > OK, so it's BASIC
    >
    >
    > geoff
    >
    >


    while (1){
    printf("Dumb\n");
    }
     
    Sailor Sam, Nov 10, 2009
    #13
  14. Lawrence D'Oliveiro

    Nik Coughlin Guest

    "Sailor Sam" <> wrote in message
    news:hdahfe$5sv$-september.org...
    > geoff wrote:
    >> John Little wrote:
    >>> On Nov 9, 7:11 pm, Lawrence D'Oliveiro <l...@geek-
    >>> central.gen.new_zealand> wrote:
    >>> ...
    >>>> However, in this case, that last expression happens to be a GOTO
    >>>> statement. So something funny happens...
    >>> In short, "goto" is evil.
    >>>
    >>> Regards, John

    >>
    >> 10 Print "Dork"
    >> 20 Goto 10
    >>
    >> What does this print ?
    >>
    >> OK, so it's BASIC
    >>
    >>
    >> geoff

    >
    > while (1){
    > printf("Dumb\n");
    > }


    for(;;)printf("Yep\n");
     
    Nik Coughlin, Nov 10, 2009
    #14
  15. Lawrence D'Oliveiro

    EMB Guest

    geoff wrote:
    >
    > 10 Print "Dork"
    > 20 Goto 10
    >
    > What does this print ?


    I believe it prints an approximation of "Lawrence d'Oliveiro" ad nauseum.
     
    EMB, Nov 10, 2009
    #15
  16. Lawrence D'Oliveiro

    John Little Guest

    On Nov 10, 2:06 pm, Lawrence D'Oliveiro <l...@geek-
    central.gen.new_zealand> wrote:

    > > In short, "goto" is evil.

    >
    > Yeah, but do you realize _how_ evil?


    (I was speaking generally, not just Algol, which I've never used.)
    Oh, bitterly yes, I learned very hard just how bad the construct is,
    dealt to me by others. Often those without those scars assume "evil"
    is hyperbole, vain posturing, but the word is appropriate for
    something so malign.

    Sure there's globals, self-modifying code, non-atomic attributes, and
    the like, but goto ...

    John
     
    John Little, Nov 11, 2009
    #16
  17. Lawrence D'Oliveiro

    Enkidu Guest

    John Little wrote:
    > On Nov 10, 2:06 pm, Lawrence D'Oliveiro <l...@geek-
    > central.gen.new_zealand> wrote:
    >
    >>> In short, "goto" is evil.

    >> Yeah, but do you realize _how_ evil?

    >
    > (I was speaking generally, not just Algol, which I've never used.)
    > Oh, bitterly yes, I learned very hard just how bad the construct is,
    > dealt to me by others. Often those without those scars assume "evil"
    > is hyperbole, vain posturing, but the word is appropriate for
    > something so malign.
    >
    > Sure there's globals, self-modifying code, non-atomic attributes, and
    > the like, but goto ...
    >

    Cobol had a 'goto depending on <variable>' statement. Now THAT is pure evil.

    Cheers,

    Cliff

    --

    The Internet is interesting in that although the nicknames may change,
    the same old personalities show through.
     
    Enkidu, Nov 11, 2009
    #17
  18. In message <>, John Little wrote:

    > On Nov 10, 2:06 pm, Lawrence D'Oliveiro <_zealand> wrote:
    >
    >> > In short, "goto" is evil.

    >>
    >> Yeah, but do you realize _how_ evil?

    >
    > Oh, bitterly yes, I learned very hard just how bad the construct is,
    > dealt to me by others.


    More modern language eschew them, but then they bring in exceptions, which
    seem to be just as evil.
     
    Lawrence D'Oliveiro, Nov 12, 2009
    #18
  19. In message <>, Allistar wrote:

    > In what way are exceptions evil?


    With a goto, at least when you saw "goto there", you could statically
    examine the source code looking for a relevant occurrence of "there:", so
    you knew where it would jump to--there would always be only one possibility.

    But with an exception, when you see "raise HandleThis", you cannot in
    general determine just from looking at the source code which instances of
    "try ... except HandleThis" are going to catch that exception. That depends
    on what happens dynamically at runtime, on which functions call which other
    functions.
     
    Lawrence D'Oliveiro, Nov 12, 2009
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cGVjaw==?=

    Can't print to print server

    =?Utf-8?B?cGVjaw==?=, Feb 2, 2005, in forum: Wireless Networking
    Replies:
    2
    Views:
    726
    =?Utf-8?B?cGVjaw==?=
    Feb 3, 2005
  2. =?Utf-8?B?enljZ3M=?=
    Replies:
    1
    Views:
    577
    Daz N
    Jan 4, 2005
  3. epl

    Print sharing -- why can't I print?

    epl, Sep 14, 2003, in forum: Computer Support
    Replies:
    1
    Views:
    473
  4. vddiee
    Replies:
    2
    Views:
    2,923
    vddiee
    Jul 16, 2004
  5. Bun Mui
    Replies:
    3
    Views:
    902
    Phantom
    Sep 13, 2004
Loading...

Share This Page