Routines like this are why you don't need loops in Haskell. You have prefabricated higher level ways of dealing with lists. Many (most?) times when you are using a "for" loop in some other language you are cranking an index variable (often named "i") from 1 to N. Why not just get rid of that -- let the language do the work for you? A good step in that direction is something like a "foreach" interator that runs through a list or some collection.
ll = [1,3,7,99] xx = map show ll main = putStrLn $ show xx -- yields: ["1","3","7","99"]Here "show" is a function that converts most anything (or tries to) into a string. We map it over the list ll, converting each integer in the list into a string. We need to use show again to print the list -- the function "putStrLn" expects a string argument, not a list.
lll = [1,3,8,99] main = putStrLn $ show $ filter isOdd lll -- yields: [1,3,99]Note here the use of the "$" operator (function). putStrLn expects a single argument and will just grab "show" without the dollar sign. The dollar sign effectively says, "completely do all the stuff to the right of the dollar sign first" then gives the result to PutStrLn. This is typical Haskell.
Ruby has both map and filter (and provides "select" as a synonym for filter). Javascript has them too. C# has the functionality but with different names (typical Microsoft).
Tom's software pages / tom@mmto.org