Hacker News new | ask | show | jobs
by tome 36 days ago
Haskell most definitely supports loops:

    ghci> :{
    ghci| for_ [1 .. 10] $ \i ->
    ghci|   print i
    ghci| :}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
It doesn't require loops to be implemented at the language level, however. The language is rich enough to support user-defined loop constructs.
2 comments

Digging through the current Haskell standard library - for_ calls traverse_, which calls foldr, which calls foldMap, which calls… foldr. Found the recursion! The person you’re replying to is correct - you can mimic the style of a for loop, but under the hood all of this stuff is going to be implemented with recursion (and tail recursion, if you don’t want to hate your life).

I also may have gotten some detail slightly wrong, the code in Data.Foldable is kinda weird and I haven’t Haskell’d in a while

Loop constructs like whileM_ require extra IORef effort to the point where people actually recommend recursion (with tail recursion) instead, which is still harder to implement than conventional while loops.