The LOLCODE forum

Discussions in support of the LOLCODE.com wiki

You are not logged in.

Announcement

New registrations are disabled. Please see this announcement.

#1 2007-07-09 09:57:38

JennyB
Member
From: Co Fermanagh, N. Ireland
Registered: 2007-07-04
Posts: 27

Loops

Code:

IM IN YR <label> <operation> YR <variable> [TIL|WILE <expression>]
  <code block>
IM OUTTA YR <label>

I take it that the variable can be used as an index within the loop, but there seems no way to specify a start value.
How about this variant?

Code:

IM IN YR <variable> <operation> YR <start epression> [TIL|WILE <expression>]
  <code block>
IM OUTTA YR <variable>

As in:

Code:

HOW DUZ I COUNTDOWN YR Start
   IM IN YR beat  NERFIN YR Start TIL 0
       VISIBLE  beat "! "!
   IM OUTA YR beat
IF U SAY SO

COUNTDOWN 10

10! 9! 8! 7! 6! 5! 4! 3! 2! 1! 0!

Last edited by JennyB (2007-07-09 09:59:07)


Test all things - hold fast to that which is good.

Offline

 

#2 2007-07-09 12:17:25

risser
Moderator
Registered: 2007-06-01
Posts: 469

Re: Loops

Nice.

Offline

 

#3 2007-07-09 13:53:34

atl
BDFL
From: lolcode.com
Registered: 2007-05-31
Posts: 324
Website

Re: Loops

Okay, I get it and like it.

Can we have some opinions on fast-tracking this into 1.2 in ~24 hours?

Offline

 

#4 2007-07-09 14:21:18

Hwel
Member
From: Hamburg, Germany
Registered: 2007-06-11
Posts: 42

Re: Loops

+1 by me.

Offline

 

#5 2007-07-09 15:10:56

risser
Moderator
Registered: 2007-06-01
Posts: 469

Re: Loops

Okay, I had a look at the spec.
If you can predeclare the var, then you can give it a starting value.

Here's how it could work in the given spec:

Code:

HOW DUZ I COUNTDOWN YR start
   I HAS A BEAT ITZ start
   IM IN YR BAND NERFIN YR BEAT TIL BOTH SAEM BEAT AN 0
       VISIBLE  BEAT "! "!
   IM OUTTA YR BAND
IF U SAY SO

But we could easily add (and I think it was proposed), something like FROM:

Code:

HOW DUZ I COUNTDOWN YR start
   IM IN YR BAND NERFIN YR BEAT FROM start TIL BOTH SAEM BEAT AN 0
       VISIBLE  BEAT "! "!
   IM OUTTA YR BAND
IF U SAY SO

I think the label allows you to give it flavor.  The problem is that the proposed change looks odd when you stick in numbers or other expressions, I think:

Code:

HOW DUZ I COUNTDOWN?
   IM IN YR COUNT NERFIN YR 10 TIL BOTH SAEM COUNT AN 0
       VISIBLE  COUNT "! "!
   IM OUTTA YR COUNT
IF U SAY SO

or 

HOW DUZ I FACTORIZE YR TING?
   IM IN YR COUNT UPPIN YR 2 TIL BOTH SAEM COUNT AN BIGR OF COUNT AN SQUARE_ROOT TING
       BOTH SAEM 0 AN MOD OF TING AN COUNT
       O RLY?
           YA RLY, VISIBLE COUNT "! "!
       OIC
   IM OUTTA YR COUNT
IF U SAY SO

It just doesn't read right.  But:

Code:

HOW DUZ I FACTORIZE YR TING?
   I HAS A COUNT ITZ 2
   IM IN YR FACTORIZOR UPPIN YR COUNT TIL BOTH SAEM COUNT AN BIGR OF COUNT AN SQUARE_ROOT TING
       BOTH SAEM 0 AN MOD OF TING AN COUNT
       O RLY?
           YA RLY, VISIBLE COUNT "! "!
       OIC
   IM OUTTA YR FACTORIZOR
IF U SAY SO

or 
   IM IN YR FACTORIZOR UPPIN YR COUNT FROM 2 TIL ...
                 BOTH SAEM COUNT AN BIGR OF COUNT AN SQUARE_ROOT TING

The label gives it flavor.  UPPIN YR 2 just doesn't work so good.

Offline

 

#6 2007-07-09 19:58:53

Arachnid
Member
Registered: 2007-06-02
Posts: 271

Re: Loops

Personally, I like loops having names. It gives us the possibility of named breaks and continues, too. Naming it after the variable doesn't work so well, IMO.

That said, I'm not hugely attached to that POV.


lolcode.net - .NET LOLCode compiler.

Offline

 

#7 2007-07-09 23:05:57

atl
BDFL
From: lolcode.com
Registered: 2007-05-31
Posts: 324
Website

Re: Loops

Hmm.
Loops have already been marked as immature and subject to change, so we can work on this some more.

Offline

 

#8 2007-07-10 21:15:27

JennyB
Member
From: Co Fermanagh, N. Ireland
Registered: 2007-07-04
Posts: 27

Re: Loops

Arachnid wrote:

Personally, I like loops having names. It gives us the possibility of named breaks and continues, too. Naming it after the variable doesn't work so well, IMO.

That said, I'm not hugely attached to that POV.

By 'named breaks' you mean 'GTFO loop'or 'GTFO name' to branch to 'OIC name' and perhaps 'AGAEN loop for CONTINUE?  Interesting.

I'm opposed to having a label as well as a variable because then you need to think of a name for both even if your counted loop doesn't ever reference the index. 

Looking back, I realise I've just re-invented the FOR loop:

Code:

FOR   var=start  TO end [BY step] DO
IM IN YR var [UPPIN/NERFIN [YR] start] [TIL end]
...
[STEPPIN step]
...

Var is local to the loop and set to the value of start on entry. Start and end are numerical expressions, not  boolean, and evaluated only on entry to the loop. If either are missing, it is assumed to be 0.

UPPIN and NERFIN are indicators which direction the loop is meant to go. If there is no STEPPIN in the loop, then code is compiled at the end of the loop to increment var and exit the loop if it now exceeds end if UPPIN, or  to nerf it and exit if it is now less if NERFIN.  Otherwise, only an unconditional loop is compiled and the exit is handled by STEPPIN, which ups or nerfs var by step and exits if the limit is crossed. The step can vary on each iteration. Positive steps tend towards loop termination whether UPPIN or NERFIN, negative steps delay termination.

STEPPIN can appear anywhere in the loop (there may even be more than one). If the distance between var and end is less than step, then the loop is exited.

Code:

HOW DUZ I COUNTDOWN YR number  BTW prints from number-1 to 0  
   IM IN YR COUNT NERFIN YR number                     
       STEPPIN 1                              BTW prints nothing if number=0  
       VISIBLE  COUNT "! "!
                                                    BTW with no STEPPIN there would be an implicit STEPPIN 1 here
   IM OUTTA YR COUNT                  BTW and the loop would print from number to 0
IF U SAY SO

or 

HOW DUZ I FACTORIZE YR TING?
   IM IN YR FACTOR UPPIN 2 TIL  SQUARE_ROOT TING
       BOTH SAEM 0 AN MOD OF TING AN COUNT
       O RLY?
           YA RLY, VISIBLE FACTOR "! "!
       OIC                                           BTW implicit STEPPIN 1 here
   IM OUTTA YR FACTOR
IF U SAY SO

Last edited by JennyB (2007-07-11 09:18:45)


Test all things - hold fast to that which is good.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson