In: Computer Science
<Haskell>
Define an action adder :: IO () that reads a given number of
integers from the keyboard, one per line, and displays their sum.
For example: > adder
How many numbers? 5
1 3 5 7 9
The total is 25
Hint: start by defining an auxiliary function that takes the current total and how many numbers remain to be read as arguments. You will also likely need to use the library functions read and show.
Please find the haskell program and it output below:
adder :: IO ()
adder = do
x <- getLine
total <- getnums (read x :: Int)
putStrLn (" total of numbers is: " ++ show (sum total))
getnums :: Int -> IO [Int]
getnums 0 = return []
getnums x = do
ns <- getLine
let int = read ns :: Int
total <- getnums (x-1)
return (int:total)
In the output , 4 numbers are entered 3,2 ,4,5 and their sum is 14