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 = (\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:21:30:
    Couldn't match expected type `Double'
                with actual type `a0 -> ((a1 -> a1) -> t0 -> a0) -> t0 -> a0 -> a0'
    Relevant bindings include
      x1 :: forall t. a0 -> ((a1 -> a1) -> t -> a0) -> t -> a0 -> a0
        (bound at prog.hs:24:11)
    Probable cause: `x1' is applied to too few arguments
    In the first argument of `OneSolution', namely `x1'
    In the expression: OneSolution x1

prog.hs:22:31:
    Couldn't match expected type `Double'
                with actual type `a0 -> ((a1 -> a1) -> t1 -> a0) -> t1 -> a0 -> a0'
    Relevant bindings include
      x1 :: forall t. a0 -> ((a1 -> a1) -> t -> a0) -> t -> a0 -> a0
        (bound at prog.hs:24:11)
    Probable cause: `x1' is applied to too few arguments
    In the first argument of `TwoSoulutions', namely `x1'
    In the expression: TwoSoulutions x1 x2

prog.hs:22:34:
    Couldn't match expected type `Double'
                with actual type `a3 -> ((a4 -> a4) -> t2 -> a3) -> t2 -> a3 -> a3'
    Relevant bindings include
      x2 :: forall t. a3 -> ((a4 -> a4) -> t -> a3) -> t -> a3 -> a3
        (bound at prog.hs:25:11)
    Probable cause: `x2' is applied to too few arguments
    In the second argument of `TwoSoulutions', namely `x2'
    In the expression: TwoSoulutions x1 x2

prog.hs:24:16:
    No instance for (Fractional a0) arising from a use of `f'
    The type variable `a0' is ambiguous
    Relevant bindings include
      x1 :: a0 -> ((a1 -> a1) -> t -> a0) -> t -> a0 -> a0
        (bound at prog.hs:24:11)
    Note: there are several potential instances:
      instance Fractional Double -- Defined in `GHC.Float'
      instance Fractional Float -- Defined in `GHC.Float'
      instance Integral a => Fractional (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
    In the expression: f a b d (+)
    In an equation for `x1': x1 = f a b d (+)
    In an equation for `sqrtEquation':
        sqrtEquation a b c
          | d < 0.0 = NoSolutions
          | d == 0.0 = OneSolution x1
          | d > 0.0 = TwoSoulutions x1 x2
          where
              d = b * b - 4 * a * c
              x1 = f a b d (+)
              x2 = f a b d (-)
              f a b d operator
                = (\ a b d operator -> (operator - b sqrt d) / 2 * a)

prog.hs:24:24:
    No instance for (Num a2) arising from a use of `+'
    The type variable `a2' is ambiguous
    Note: there are several potential instances:
      instance Num Double -- Defined in `GHC.Float'
      instance Num Float -- Defined in `GHC.Float'
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
      ...plus three others
    In the fourth argument of `f', namely `(+)'
    In the expression: f a b d (+)
    In an equation for `x1': x1 = f a b d (+)

prog.hs:25:16:
    No instance for (Fractional a3) arising from a use of `f'
    The type variable `a3' is ambiguous
    Relevant bindings include
      x2 :: a3 -> ((a4 -> a4) -> t -> a3) -> t -> a3 -> a3
        (bound at prog.hs:25:11)
    Note: there are several potential instances:
      instance Fractional Double -- Defined in `GHC.Float'
      instance Fractional Float -- Defined in `GHC.Float'
      instance Integral a => Fractional (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
    In the expression: f a b d (-)
    In an equation for `x2': x2 = f a b d (-)
    In an equation for `sqrtEquation':
        sqrtEquation a b c
          | d < 0.0 = NoSolutions
          | d == 0.0 = OneSolution x1
          | d > 0.0 = TwoSoulutions x1 x2
          where
              d = b * b - 4 * a * c
              x1 = f a b d (+)
              x2 = f a b d (-)
              f a b d operator
                = (\ a b d operator -> (operator - b sqrt d) / 2 * a)

prog.hs:25:24:
    No instance for (Num a5) arising from a use of `-'
    The type variable `a5' is ambiguous
    Note: there are several potential instances:
      instance Num Double -- Defined in `GHC.Float'
      instance Num Float -- Defined in `GHC.Float'
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
      ...plus three others
    In the fourth argument of `f', namely `(-)'
    In the expression: f a b d (-)
    In an equation for `x2': x2 = f a b d (-)
stdout
Standard output is empty