Printing elements of a list on new lines

前端 未结 3 1828
误落风尘
误落风尘 2020-12-13 03:44

I am trying to print the elements of my list onto new lines, but i cant get it to work;

printElements :: [String] -> IO()
printElements (x:xs) =  print x          


        
3条回答
  •  半阙折子戏
    2020-12-13 04:08

    Instead of explicit recursion, you can use mapM_ to call putStrLn for every element. It works like the regular map for lists, but is used with a monadic function (thus the "M"). The underscore variant is used when you only care about the side-effect (in this case, printing) and don't care about the result of the mapped function.

    printElements :: [String] -> IO ()
    printElements = mapM_ putStrLn

提交回复
热议问题