Loop without 'looping'

  • A question similar to this has been asked a couple of years ago, but this one is even trickier.

    The challenge is simple. Write a program (in your language of choice) that repeatedly executes code without using any repetition structures such as while, for, do while, foreach or goto (So for all you nitpickers, you can't use a loop). However, recursion is not allowed, in the function calling itself sense (see definition below). That would make this challenge far too easy.

    There is no restriction on what needs to be executed in the loop, but post an explanation with your answer so that others can understand exactly what is being implemented.

    For those who may be hung up on definitions, the definition of a loop for this question is:

    A programming language statement which allows code to be repeatedly executed.

    And the definition of recursion for this question will be your standard recursive function definition:

    A function that calls itself.

    Winner will be the answer that has the most upvotes on July 16th at 10 AM eastern time. Good luck!


    To calm confusion that is still being expressed this may help:

    Rules as stated above:

    • Don't use loops or goto

    • Functions cannot call themselves

    • Do whatever you want in the 'loop'

    If you want to implement something and the rules don't explicitly disallow it, go ahead and do it. Many answers have already bent the rules.

    What is the definition of a loop?

    The bash 'yes' command would do, right?

    The definition of a loop for this question will be `A programming language statement which allows code to be repeatedly executed`

    hmm.. shame you had to post this comment.. just noted something you didnt mention in the structures you cant use XD

    I think it should be possible to copy over a lot of answers from this question.

    This might sound stupid but what is the exact definition of recursion? Is that just a function calling itself?

    @user24925 How sneeky and crappy can the answer be?

    Would creating new functions with the same content and then repeatedly calling them be considered recursion? What about a program compiling a program then calling that program?

    As the existing comments and answers demonstrate, it's not clear what is covered by the "etc." in the forbidden structures.

    The question has been updated for a focus on creativity. The goal here is to be creative and learn tricks that other programming languages might have (like the 'yes' command in BASH).

    @TeunPronk I added a definition of recursion to the question. It's just your basic, function calling itself definition.

    For those who want an easy trick, i can't be bothered posting it :P Just make 2 functions, `function A` calls `function B` and `function B` calls `function A` while 1 of the functions performs something. Since the function doesn't call itself it should be valid based on the criteria ^.^

    "Changed to popularity contest for a focus on creativity" Changing the question is cheating!

    @CousinCocaine The question and its requirements were not changed, only the winning guidelines from code golf to popularity contest.

    @CailinP a winning answer is not a winning answer anymore. That is changing the outcome by changing the question.

    For the people who are complaining that the question is unclear, what is unclear about it?

    Do vectorized functions count as loops? Otherwise R has this with `apply`

    The definition of "recursion" isn't very useful. It would be better to disallow *recursive functions*, which are functions that refer to themselves, directly or indirectly.

    What is unclear is the "definitions" of loop constructor and recursion. Neither are very precise. Example: `rep(f){f();f();}` - this is a statement (a function declaration is a statement in some languages) that allows executing code repeatedly. Is it disallowed. You ask for code to implement a loop. If that code is syntactically a statement, you have just disallowed it. Another example: `f(b) { b(); g(b); }; g(b) { f(b); }`. I'd say `f` is a recursive function (by being mutually recursive with `g`). Is it disallowed?

    What is unclear hasn't changed since my first comment: what is permitted and what is forbidden. And there's no reasonable way to fix it without posting all the permitted answers in the question, which rather defeats the object.

    Please don't get hung up on what is and is not permitted. As stated above: No loops or goto, Functions cannot call themselves, Do whatever you want in the 'loop.' If you want to implement something and the rules don't explicitly say no to it, go ahead and do it. Many answers have already bent the rules.

    What about a clock? Such as `tty-clock -D`? Or is that too lame ;)

    @CailinP, what I'm "*hung up on*" is that questions on the site should be on topic for the site: that means having a clear, objective specification, which this question does not.

    I don't think that this question has an answer. Even if you don't write and exact loop or recursion, it is hidden somewhere inside.

    I'm too lazy to go through all the answers... is there a `crontab` solution yet?

  • histocrat

    histocrat Correct answer

    7 years ago


    def method_missing(meth,*args)
    puts 'Banana'

    def also
    puts "Orange you glad I didn't say banana?"



    Clears its throat, prints "Banana" 3070 times, and also puts "Orange you glad I didn't say banana?".

    This uses Ruby's ridiculous just-in-time method definition functionality to define every method that lies alphabetically between the words 'ahem' and 'also' ("ahem", "ahen", "aheo", "ahep", "aheq", "aher", "ahes", "ahet", "aheu", "ahev"...) to first print Banana and then call the next in the list.

    Why does it end?

    It eventually hits "also", which is defined, and therefore not missing.

    Ah, now I see it. Somehow missed the last line. Nice one, have a plus one.

    This is hysterical.

    @barrycarter: In Ruby, `String#next`, which is called in `method_missing` functions more or less like adding 1 to a number except it works with all alphanumeric characters (and non-alnums if they are the only characters in the string). See http://www.ruby-doc.org/core-2.1.2/String.html#method-i-next

    I don't really get the point of `method_missing` in Ruby; I heard about it in the Wat talk and thought was just as bad a JS's notion of addition.

    @NickT it is usable in classes like XML builders where you can any tag created only by `b.my_tag`. It also is used in ActiveRecord models or `OpenStruct`. In 'Wat talk' he say that global `method_missing` is bad, but scoped is awesome.

    I remember an old comment on another ruby program: "I like it because it has meth"

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM