4 ms·
mapM_ print [1..1000] Yay Haskell. (Original question is for C/C++, so obviously this is a bogus answer.)
by tumult 16y ago
mapM_ print [1..1000]
Yay Haskell.
(Original question is for C/C++, so obviously this is a bogus answer.)
- tptacek 16y agomapm_ is also looping construct.
- Confusion 16y ago'without using loops' is very ill defined. All solutions that in some way generate all integers up to a thousand need to include something we would, in English, refer to as 'a loop'. The ctor trick also contains 'a loop'. What solutions would you consider valid, without resulting in semantic squabbling over whether 'i < 1000' is a conditional or a condition and whether that matters?
- tptacek 16y agoWhere's the loop in the array initialization? Where's the loop in "map"? That's why one's clever and the other's a cheat.
- Raphael_Amiard 16y agoThe loop in the array initialization is generated by the compiler for the static version, by the runtime for the dynamic allocation version. It's still a loop. You're just re-using a loop already implemented in the runtime. That's quite the same thing as using map, though a tiny bit clever. But if you qualify map as cheat, well then the constructor trick is just a cleverer cheat.
- petercooper 16y agoNot entirely dissimilar in Ruby: p [*1..1000] Or being anal about the formatting: puts [*1..1000].join ' '