Your program currently has a type error because it's trying to print an IO [Int] directly. As the error suggests, you need to perform the IO action and handle its result, rather than trying to print the action itself.
The function numbers is designed to collect integers until a 0 is encountered, at which point it returns the collection. The final result of numbers is an IO action that yields a list of integers, which is why you have the type error when you try to print numbers [] directly.
In order to fix this, you need to first perform the IO action, extract the list, and then print the list. Also, make sure to count the additions performed (i.e., the length of the list minus one for the terminating zero).
Here's the corrected version of your code that addresses these issues:
module Task31 where
--import Prelude hiding (IO, getChar, getLine, readLn, -- remove this line to test locally
-- putChar, putStr, putStrLn, print) -- remove this line to test locally
--import Test.IOTasks.IOrep -- remove this line to test locally
--type IO = IOrep -- remove this line to test locally
{- In the following IO programming task, you can use the primitives
- listed above, as well as 'return'. If you want to test your
- submission locally, simply remove all the marked lines above. But
- for Autotool to accept your submission, those lines should be put
- back in, exactly as they are above.
-
- Moreover, if you first want to test locally, then depending on your
- operating system and settings, you might have to additionally add
- an 'import System.IO' and start your program in 'main' with the
- statement 'hSetBuffering stdout NoBuffering'. (In Autotool that
- statement has no effect and can be omitted, but does not hurt
- either. However, the potentially added line 'import System.IO' has
- to be removed before uploading to Autotool.)
-}
{- Write a program that reads in two integers (negative integers too)
- and prints out their sum. This behavior is repeated until the first
- of the two numbers read is 0. The program then terminates (not
- reading a second number), printing the count of additions
- performed.
-
- You can add additional information to both the output of the
- addition results as well as the final output. Furthermore you might
- want to add additional outputs to indicate what the user has to do
- next.
-}
main :: IO ()
main = do
count <- performAdditions 0
putStrLn $ "Number of additions performed: " ++ show count
performAdditions :: Int -> IO Int
performAdditions count = do
putStrLn "Enter the first number (0 to terminate):"
x <- readLn
if x == 0
then return count
else do
putStrLn "Enter the second number:"
y <- readLn
let sum = x + y
putStrLn $ "Sum: " ++ show sum
performAdditions (count + 1)
Explanation:
- The
main function now calls performAdditions with an initial count of 0. This will track the number of additions performed.
- The
performAdditions function reads the first number and checks if it is 0. If it is, it returns the current count.
- If the first number is not 0, it reads the second number, calculates the sum, and prints it.
- Then it recursively calls
performAdditions, incrementing the count by 1.
This will repeat until the user enters 0 as the first number, at which point the program will print the total number of additions performed and terminate.