Live data from Hacker News

Display 100 Hello World Without Using Loops

ajibanda.com

21–30 of 42 posts

Re: Display 100 Hello World Without Using Loops

#21
post #4
post #3

Python version: exec "print 'Hello world'\n" * 100

that reminds me why I don't like Python. Ad-hoc tools (many) instead of a few general concepts working well together. Btw at interview time this solution would not be acceptable, because you are using still a built-in language construct for looping.

On the contrary, wouldn't OP's solution be an example of more generalized implementation of multiplication? I can see that it is similar to (X * 2) where X defines behavior of __mul__.

Yes, there is chance that arbitrary objects can provide different semantics for __mul__. It would be true for any language.

Re: Display 100 Hello World Without Using Loops

#22
post #4

Earlier quoted context omitted.

that reminds me why I don't like Python. Ad-hoc tools (many) instead of a few general concepts working well together. Btw at interview time this solution would not be acceptable, because you are using still a built-in language construct for looping.

Huh? This is a specific case of the * operator, which is overloaded for type string to return a repetition of the string. The only ad-hoc in the example is the print statement, which is gone in 3.2. How do you know the __mul__ operator overload for string is implemented with iteration?

> This is a specific case of the * operator, which is overloaded

It is not operator overloading in the strictest sense, but more of duck typing. In Python (and Ruby), operators are syntactic sugar for method calls on the first operand.

Operator overloading on the contrary suggests a function add(a, b) that reacts differently through polymorphism (i.e according to the types of its arguments).

The distinction is important as overloading and polymorphism simply do not exist in Ruby and Python, only overriding.

Re: Display 100 Hello World Without Using Loops

#23

100.times {puts "Hello, world"} I'm just sayin'.

I'm not sure this will reflect well on Haskell that this is the most natural way I can think to write it. But I can pretty much guarantee replicateM_[0] is the most generalizable idiom in this whole comments section.

    replicateM_ 100 $ print "Hello World!"
It's pretty to me, damnit.

-----

[0] http://hackage.haskell.org/packages/archive/base/latest/doc/...

Re: Display 100 Hello World Without Using Loops

#27
post #17
post #2

Decent enough explanation of recursion. Why not take it further and produce a more general solution? In JavaScript I'd write something like this: function recurseBetween(start, end, callback) { // Create a recursive function // which checks the limits and calls the supplied callback var recursiveCallback = function(i) { // Call the original callback callback(i); // If we're at the end, stop if(i >= end) { return; } e…

Or go back to C and learn that recursion is not really used like that. If you know the limits beforehand, use "for". If you don't know how deep the processing will go, use recursion. If you must code a loop without using "for" it means you're doing a homework ;)

> Or go back to C and learn that recursion is not really used like that.

It's not used like that in C, it's definitely used like that in Erlang.

Re: Display 100 Hello World Without Using Loops

#28

Earlier quoted context omitted.

sequence_ $ take 100 $ repeat $ putStrLn "Hello, World" alternatively, mapM_ putStrLn $ take 100 $ repeat "Hello, World"

[see above]

Oh, didn't know about `replicateM_`. Not sure I like it much though, seems... redundant.

Re: Display 100 Hello World Without Using Loops

#30

Earlier quoted context omitted.

[see above]

Oh, didn't know about `replicateM_`. Not sure I like it much though, seems... redundant.

It's a pretty common pattern, I think it's worth its own word, at least as much as mapM:

    mapM f xs      = sequence (map f xs)
    replicateM n x = sequence (replicate n x)
Another cool thing replicateM is equivalent to: for lists, it's the cartesian power (sequence is the cartesian product). ...this is actually the primary reason I'm aware of it.
Post reply on HN