Начал изучать Haskell, попробовал реализовать на нём простейший метод бисекции, но что-то не так с типами. Вот код:
mpd a b e =
let
c = (a+b) / 2
func x = 10*x + x^2 + (2*x)^((-x+2)^3) - 8
in
if abs (b-a) > e then
if func a * func c < 0 then
mpd a c e
else
mpd c b e
else
c
main = putStrLn $ "x = " ++ show (mpd (-100000) 100000 0.000001)
На выражении "x - 5" работало, а на этом перестало компилироваться. Падает с ошибками:
main.hs:14:29: No instance for (Show a0) arising from a use of
show' The type variable
a0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Show Double -- Defined inGHC.Float' instance Show Float -- Defined in
GHC.Float' instance (Integral a, Show a) => Show (GHC.Real.Ratio a) -- Defined inGHC.Real' ...plus 23 others In the second argument of
(++)', namelyshow (mpd (- 100000) 100000 0.000001)' In the second argument of
($)', namely `"x = " ++ show (mpd (- 100000) 100000 0.000001)' In the expression: putStrLn $ "x = " ++ show (mpd (- 100000) 100000 0.000001)main.hs:14:35: No instance for (Integral a0) arising from a use of
mpd' The type variable
a0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Integral Int -- Defined inGHC.Real' instance Integral Integer -- Defined in
GHC.Real' instance Integral GHC.Types.Word -- Defined inGHC.Real' In the first argument of
show', namely(mpd (- 100000) 100000 0.000001)' In the second argument of
(++)', namelyshow (mpd (- 100000) 100000 0.000001)' In the second argument of
($)', namely `"x = " ++ show (mpd (- 100000) 100000 0.000001)'main.hs:14:40: No instance for (Num a0) arising from a use of syntactic negation The type variable
a0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Num Double -- Defined in
GHC.Float' instance Num Float -- Defined inGHC.Float' instance Integral a => Num (GHC.Real.Ratio a) -- Defined in
GHC.Real' ...plus three others In the first argument ofmpd', namely
(- 100000)' In the first argument ofshow', namely
(mpd (- 100000) 100000 0.000001)' In the second argument of(++)', namely
show (mpd (- 100000) 100000 0.000001)'
mpd
. Видимо, такой: mdp :: Double -> Double -> Double -> Double – andrybak 13 мар '14 в 13:38func' Possible fix: add an instance declaration for (Integral Double) In the first argument of
(*)', namelyfunc a' In the first argument of
(<)', namely `func a * func c' In the expression: func a * func c < 0 – имя слишком коротко 13 мар '14 в 13:44