How to print output on a single line?

Viewed 66

Today I was working on this code which outputs the average value of a series of "arrays", the data is inputted in this format:

3               #Number of arrays to get the average from
2   3   0       #First array
4   5   0       #Second array
1   4   5   0   #Third array

I worked on a code that outputs the data, but realized that it prints it like this:

2  #Average (Int) of the 1st array
4  #Average (Int) of the 2nd array
3  #Average (Int) of the 3rd array

(Take into account that the 0 at the end of every array is not used when calculated the average, it only exists for means of indicating end of array)

My question is, How can I properly change my code so that I can output the data like this? :

2  4  3

Here is the code I've been working in:

sumList :: [Int] -> Int sumList []    = 0 sumList (u:v) = u + sumList v

funavg :: Int -> IO () funavg numitint = if numitint==0 then return ()
                                else do
                                arrs <- getLine
                                let arrnum = (map read(words arrs) :: [Int])
                                let total = sumList arrnum
                                let avg = div total ((length arrnum)-1)
                                print avg
                                funavg (numitint - 1)

main :: Prelude.IO () main = do   
  numits <- getLine   
  let numitint = read numits :: Int   
  funavg numitint

I've searched many documents and websites, but can't come with an ideal answer. Using recursion is mandatory.

Any help is highly appreciated :D

1 Answers

print is equivalent to putStrLn . show and is provided for convenience to print a single value of any Show type.

print does not have a standard library companion which omits the newline, but putStrLn does: it's called putStr. Instead of print avg, consider

putStr $ show avg
Related