fork download
  1. import System.IO
  2. import Text.Read
  3.  
  4. data Solution =
  5. OneSolution Double
  6. | TwoSoulutions Double Double
  7. | NoSolutions deriving (Show)
  8.  
  9. promptDouble :: String -> IO Double
  10. promptDouble text = do
  11. putStr text
  12. hFlush stdout
  13. line <- getLine
  14. case readMaybe line of
  15. Just x -> return x
  16. Nothing -> putStrLn "Invalid number entered" >> promptDouble text
  17.  
  18. sqrtEquation :: Double -> Double -> Double -> Solution
  19. sqrtEquation a b c
  20. | d < 0.0 = NoSolutions
  21. | d == 0.0 = OneSolution x1
  22. | d > 0.0 = TwoSoulutions x1 x2
  23. where d = b*b - 4*a*c
  24. x1 = f a b d (+)
  25. x2 = f a b d (-)
  26. f a b d operator = (operator (-b) sqrt d)/2*a
  27.  
  28. main :: IO ()
  29. main = do
  30. a <- promptDouble "Input a: "
  31. b <- promptDouble "Input b: "
  32. c <- promptDouble "Input c: "
  33. let result = show (sqrtEquation a b c)
  34. putStrLn result
  35.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )

prog.hs:24:24:
    Couldn't match type `Double' with `a0 -> a0'
    Expected type: Double -> (a0 -> a0) -> Double -> Double
      Actual type: Double -> Double -> Double
    In the fourth argument of `f', namely `(+)'
    In the expression: f a b d (+)

prog.hs:25:24:
    Couldn't match type `Double' with `a1 -> a1'
    Expected type: Double -> (a1 -> a1) -> Double -> Double
      Actual type: Double -> Double -> Double
    In the fourth argument of `f', namely `(-)'
    In the expression: f a b d (-)
stdout
Standard output is empty