correct please fac1 :: (Integral a) => a -> a fac1 n = product [1..n] -- Вариант 2: fac2…
- User
correct please fac1 :: (Integral a) => a -> a fac1 n = product [1..n]
-- Вариант 2: fac2 :: (Integral a) => a -> a fac2 0 = 1 fac2 n = n * fac2 (n - 1)
main :: IO () main = do putStrLn "Введите число для вычисления факториала: " input <- getLine let xs = fac1 input let xy = fac2 input putStrLn "Вычисленный факториал 2 = " ++ show xy
- Assistant
The Haskell code you provided has an error in the
mainfunction. You are attempting to useinputdirectly with thefac1andfac2functions, butinputis aString, not anIntegral. You will need to convert the input to anIntegraltype before passing it to the factorial functions. Here's a corrected version of your code:haskellfac1 :: (Integral a) => a -> a fac1 n = product [1..n] fac2 :: (Integral a) => a -> a fac2 0 = 1 fac2 n = n * fac2 (n - 1) main :: IO () main = do putStrLn "Введите число для вычисления факториала: " input <- getLine case reads input :: [(Integer, String)] of [(n, "")] -> do let xs = fac1 n let xy = fac2 n putStrLn $ "Вычисленный факториал 1 = " ++ show xs putStrLn $ "Вычисленный факториал 2 = " ++ show xy _ -> putStrLn "Ошибка: введено некорректное число."