Printing elements of a list on new lines

前端 未结 3 1839
误落风尘
误落风尘 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:28

    Your function is:

    printElements :: [String] -> IO()
    printElements [] = return ()
    printElements (x:xs) = do putStrLn x
                              printElements xs
    

    If you already know about monads, you can use mapM_ function:

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

    Note: maybe you would have to read the chapter 8 of lyah.

提交回复
热议问题