user15002350
user15002350

Reputation:

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?

Upvotes: 1

Views: 467

Answers (2)

Will Ness
Will Ness

Reputation: 71119

One way to do it is

> mapM_ putStr (intersperse " " (map show [1,2,3])) >> putStrLn ""
1 2 3
it :: ()

This prints the elements one by one, converted to strings by show, interspersed with the spaces in between.

> intersperse " " (map show [1,2,3])
["1"," ","2"," ","3"]
it :: [[Char]]

mapM_ maps an IO action constructor putStr :: String -> IO () on each of the elements, and executes them all as one combined sequence of actions.

Finally this code prints the newline.

Upvotes: 1

chepner
chepner

Reputation: 532268

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))

Upvotes: 4

Related Questions