Search code examples
haskellfunctional-programmingoutputio-monad

How to print individual elements of a list in Haskell?


I am a beginner in Haskell and I wanted to know if it was possible to print individual elements of a given list. I tried solving this problem but have failed. This is the code:

main :: IO()
main = do
    let list = [1,2,3]
    let size = length list
    let temp = print_elem list size
    print temp


print_elem :: [Int] -> Int -> Int
print_elem xs x = do
    let size = length xs
    let idx = size - x
    let element = xs !! idx
    putStr (show element)
    putStr(" ")
    let dummy = print_elem (xs (x-1))
    return " "

I wanted to print something like this

1 2 3

If I simply use putStr (show list) it will display [1,2,3] and I don't want that.

But when I run this code multiple errors occur

printelem.hs:14:5: error:
    * Couldn't match expected type `Int' with actual type `IO b0'
    * In a stmt of a 'do' block: putStr (show element)
      In the expression:
        do let size = length xs
           let idx = size - x
           let element = xs !! idx
           putStr (show element)
           ....
      In an equation for `print_elem':
          print_elem xs x
            = do let size = ...
                 let idx = ...
                 let element = ...
                 ....
   |
14 |     putStr (show element)
   |     ^^^^^^^^^^^^^^^^^^^^^

printelem.hs:16:29: error:
    * Couldn't match expected type `Int -> [Int]'
                  with actual type `[Int]'
    * The function `xs' is applied to one argument,
      but its type `[Int]' has none
      In the first argument of `print_elem', namely `(xs (x - 1))'
      In the expression: print_elem (xs (x - 1))
   |
16 |     let dummy = print_elem (xs (x-1))
   |                             ^^^^^^^^

How do you fix this issue?


Solution

  • You are thinking too imperatively. First, you need a list of strings, not a list of integers. That's map:

    > map show [1,2,3]
    ["1","2","3"]
    

    Next, you want to join them into a single, space-separate string. That's Data.List.intercalate:

    > import Data.List
    > intercalate " " (map show [1,2,3])
    "1 2 3"
    

    which you can then pass to putStrLn (print would give you the string representation of the string you already have):

    import Data.List
    
    main :: IO()
    main = do
        let list = [1,2,3]
        putStrLn (intercalate " " (map show list))