Permalink
Loops in Haskell
09 SEP 2003
I noticed in Erik Meijer's blog a link to a post Guido van Rossum made a few months back in which he asserted that looping was a higher level, more intuitive concept than recursion, and that Haskell was doomed to remain an academic language because it lacks looping constructs.
Well, if that's all that's missing, I'm here to help.
import Data.IORef
-- Define 'foreach'
foreach = flip mapM_
-- Define 'while'
while test action = do
val <- test
if val then do {action;while test action}
else return ()
-- Some helpers for use with 'while':
incr ref = modifyIORef ref (+1)
test ref f = do { val <- readIORef ref; return (f val) }
-- Exercise them. Equivalent Python code:
-- for x in range(1,11):
-- print x
-- i = 0
-- while i < 5:
-- print "Still running"
-- i += 1
main = do
foreach [1..10] print
ref <- newIORef 0
while (test ref (< 5))
(do
print "Still running!"
incr ref)
