Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I certainly wouldn't think it's a drag to use just one kind of loop: it can be kind of elegant. However, the rest of my life is just too long, my opinion on which single loop is elegant varies with time. Also I wouldn't use a single loop just because I'm forced to it: I like TIMTOWTDI languages that don't force me to use a single programmign style.

Scheme basically has one kind of looping construct: tail recursion. However, it has some advanced special forms built upon tail recursion. It has let which is just a short notation for tail recursion. But the most important looping construct is do which is by itself so powerful that one could program using only it. However, using do comes out really good only if you're doing side-effectless programming, which I don't always like. (There's a do form in common lisp too, which is similar in form but very different deep down. That do <disgust>changes the values of loop variables</disgust>, so it's not side-effect-less, and has nothing to do with tail recursion which Common lisp doesn't have anyway.)

What I've said above is not entirely true: there's one more way of looping in scheme apart from calls and tail calls: call-with-current-continuation is by itself a universal looping construct. It's however not something I'd use as a single looping construct in my whole life. (Even worse than that would be to use only setjmp/longjmp in C.)

If my mind is in a low level state, I could live with only conditional gotos.

If I program C, I'd definitely go with for. (That's what I've voted for.) It's just much more general than while. To tell the truth, just about any looping construct is more general than while. It's also nice that you don't have to look at keywords if you only use if and for as they have different number of argumets. So, I definitely don't like while. (Update: I forgot to mention it, but I did mean using only for seriously: Re: Pattern matching)

At one time, I thought that the best looping construct was { ...; ... and last; ...; redo }. This is not only my mania, as the while loop in bash, and the \loop ... \repeat macros in Plain TeX work like this.

At that time, I actually wrote a toy language where the only looping construct was ( COMMANDS; *) which executed COMMANDS repeatedly. You can exit from it with the COND ? RET operator which exited from the innermost pair of parentheses iff COND stands, and make the result of the parenthisized expression be RET.

This is actually a bit more general than the above, because you can use more of these statements in a single loop. This ? operator also served as the single way to do conditional expressions, as you can use it in a non-looping pair of parentheses, so (COND? THEN_PART; ELSE_PART) is a conditional expression. If COND is true then the THEN_PART is evaluated, and than the question mark exists from the parenthesis, so ELSE_PART isn't evaluated. If COND is false, than THEN_PART is not run, so execution proceeds to ELSE_PART. It's easy to do if-elseif constructs the same way. The question mark was chosen for this shallow analogy with (COND ? THEN_PART : ELSE_PART) in C or perl. (The language also has functions, but not tail calls. I haven't implemented that the functions could receive arguments, but that can be worked around using lexical variables and assignments. There's also a syntactic sugar COND ! RET which does the same as ? but negates the COND. To tell the truth, I've added short-circuitting and and or conditional operators in later versions, which sort of breaks the elegance of ? and !.)

I certainly wouldn't choose foreach as the single looping construct, as I've learnt that it wouldn't give me a Turing-complete language. In logician terms, we say that you can build constructive languages from foreach, which is a true subset of the set of recursively enumerable languages.

Update 2010-10-17: see also the other poll Re: My favorite looping mechanism in Perl is:.


In reply to Re: If I was forced to use only one kind of loop for the rest of my days it would be a by ambrus
in thread If I was forced to use only one kind of loop for the rest of my days it would be a by collin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (12)
    As of 2019-10-18 12:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?