Do notice "The average is 4.5 \nThe maximum is 7.0\nThe minimum is 2.0\n"
is actually not printed
Reformatting readDoubles
:
readDoubles s1 s2 = do
putStr (s1++"\n"++s2)
x <- readDoublesHelper
return ("The average is " ++ (show (average x)) ++ " \n" ++ "The maximum is " ++ (show (maximum x)) ++ "\n" ++ "The minimum is " ++ (show (minimum x)) ++ "\n")
We can see that result is “returned” rather than “printed”
As the type signature of readDoubles
is:
readDoubles :: String -> String -> IO String
It just returns a lifted string. Since GHCi
will print out the final result of the function, you’re getting "The average is 4.5 \nThe maximum is 7.0\nThe minimum is 2.0\n"
as output (the return value of the function)
However, from you description, I guess you just like to print out the result, so it should be:
readDoubles :: String -> String -> IO ()
readDoubles s1 s2 = do
putStr (s1++"\n"++s2)
x <- readDoublesHelper
putStrLn ("The average is " ++ (show (average x)) ++ " \n" ++ "The maximum is " ++ (show (maximum x)) ++ "\n" ++ "The minimum is " ++ (show (minimum x)) ++ "\n")
CLICK HERE to find out more related problems solutions.