Haskell: What does the compiler mean by this?

Hello,

once the code:

 module Task29 where import System.IO main :: IO () main = do  hSetBuffering stdout NoBuffering  -- needed depending on your operating system and settings  putStrLn "Bitte gebe die erste Zahl ein:"  x1 <- readLn  putStrLn "Zweite"  x2 <- readLn  let sum =  x1 + x2  print ("Die Summe ist:" ++ show(sum))  result <- while (x1,1) (\x -> x/=0) (\(x,count) -> do     putStrLn "Bitte gebe die erste Zahl an"                                                            x1 <- readLn                                                            putStrLn "Zweite"                                                            x2 <- readLn                                                            let sum = x1 + x2                                                            print ("Die Summe ist:" ++ show(sum))                                                            return (x1,(count+1)))  print ("Anzahl additionen:" ++ show(snd(result)))    while :: a -> (a -> Bool) -> (a -> IO a) -> IO a while ap body = loop a                 where loop x = if px then do x'<- body x                                               loop x'                                       else return x

And once the complaint:

The same applies to let sum = x1 + x2, to show and to the 1 in the tuple (x1,1), and to /=0.

I don't understand what he's trying to tell me and how I can solve this.

Thanks in advance:))

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
1 Answer
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
BorisG2011
1 year ago

x1 and x2 are supposed to record floating point numbers, which are then added in the further course of the program execution.

The problem is that Haskell does not determine the data type of X1, x1 and therefore cannot decide which definition of readLn is to be used.

In a very shortened example, I show two possible ways:

first way: auxiliary function for reading a float value:

import System.IO

readFloat :: IO Float
readFloat = readLn

main :: IO ()
main = do
  x <- readFloat
  y <- readFloat
  let sum = x + y
  print(sum)

Second way: Use of a cast:

import System.IO

main :: IO ()
main = do
  x <- readLn :: IO Float
  y <- readLn :: IO Float
  let sum = x + y
  print(sum)

Something in the way could help in your program.