[comp.lang.misc] Should loop bodies be blocks?

jhallen@wpi.wpi.edu (Joseph H Allen) (02/24/90)

Which is preferred...  loop bodies which are blocks (I.E., with their own
scope) or loop bodies which are part of scope the loop is in?
-- 
            "Come on Duke, lets do those crimes" - Debbie
"Yeah... Yeah, lets go get sushi... and not pay" - Duke

jack@cs.glasgow.ac.uk (Jack Campin) (03/02/90)

jhallen@wpi.wpi.edu (Joseph H Allen) wrote:

> Which is preferred...  loop bodies which are blocks (I.E., with their own
> scope) or loop bodies which are part of scope the loop is in?

I can only think of one example where it makes a difference, in the
following piece of PS-algol code:

let ps := 1 :: 100 of proc ( -> int ); nullproc
! declare an array of 100 int-valued procedures,
! initializing them with the null procedure

for i = 1 to 100 do ps (i) := proc ( -> int ); i
! now the 57th procedure in the array will return 57.

! PS-algol is expression-oriented, hence no
! "return" statement in the procedure body.

This works because PS-algol puts an implicit scope round the loop body;
the value of "i" packaged in the closure is whatever value it had on that
iteration.  Without that, all the procedures in the array would return 100.

I must admit I've never yet found a practical use for this feature.  I
guess you can do the same in Scheme (which I think is more or less PS-algol
with Lisp syntax and no persistence)?  If anyone's found a use for it, it'll
be an AI hacker...

-- 
--  Jack Campin   Computing Science Department, Glasgow University, 17 Lilybank
Gardens, Glasgow G12 8QQ, Scotland   041 339 8855 x6044 work  041 556 1878 home
JANET: jack@cs.glasgow.ac.uk    BANG!net: via mcvax and ukc   FAX: 041 330 4913
INTERNET: via nsfnet-relay.ac.uk   BITNET: via UKACRL   UUCP: jack@glasgow.uucp

jk@r6.uucp (John Kewley ICL) (03/07/90)

In article <4697@vanuata.cs.glasgow.ac.uk> jack@cs.glasgow.ac.uk (Jack Campin) writes:
>
>jhallen@wpi.wpi.edu (Joseph H Allen) wrote:
>
>> Which is preferred...  loop bodies which are blocks (I.E., with their own
>> scope) or loop bodies which are part of scope the loop is in?
>
>I can only think of one example where it makes a difference, in the
>following piece of PS-algol code:
>
>let ps := 1 :: 100 of proc ( -> int ); nullproc
>! declare an array of 100 int-valued procedures,
>! initializing them with the null procedure
>
>for i = 1 to 100 do ps (i) := proc ( -> int ); i
>! now the 57th procedure in the array will return 57.
>
>! PS-algol is expression-oriented, hence no
>! "return" statement in the procedure body.
>
>This works because PS-algol puts an implicit scope round the loop body;

It sure does, right after the definition of the looping constant. (Note
that I said constant and not variable, i is a constant for each iteration of the
loop.) 

>the value of "i" packaged in the closure is whatever value it had on that

You could if you wished have had a block instead of a single statement for
the loop body, if i was redeclared in this additional scope that would
be the value of i for the rest of that block.

>iteration.  Without that, all the procedures in the array would return 100.

I might be missing something, but I cannot see why they would return 100
They would return the value of i from the outermost scope of the program,
if any existed.
>
>I must admit I've never yet found a practical use for this feature.  I
>guess you can do the same in Scheme (which I think is more or less PS-algol
>with Lisp syntax and no persistence)?  If anyone's found a use for it, it'll
>be an AI hacker...

One reason for the design of PS to be like this (Apart from the fact that its
predecessor S-Algol was defined likewise.) is so that ignorant, programmers
(like myself) who wish to write awkward code can have multiple top level 
loops in the same level of scope with the same looping constant.

	J.K.

John M. Kewley                         ICL
                                       Wenlock Way,
Tel:   (+44) 61 223 1301 X2138         West Gorton,
EXAC:  J.Kewley @ UK03                 Manchester.
Email: jk@r6.cs.man.ac.uk              M12 5DR
        J.K.
 
John M. Kewley                         ICL
                                       Wenlock Way,
Tel:   (+44) 61 223 1301 X2138         West Gorton,
EXAC:  J.Kewley @ UK03                 Manchester.
Email: jk@r6.cs.man.ac.uk              M12 5DR