correct fac1 :: (Integral a) => a -> a fac1 n = product [1..n] -- Вариант 2: fac2 ::…
- User
correct 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 code is almost correct, but there is one small mistake. When reading the input from the user with
getLine, the input is of typeString, but thefac1andfac2functions expect an input of typeIntegral a. Therefore, you need to convert the input fromStringto the appropriate type before passing it to the fac1 and fac2 functions.Here is the corrected code:
correct :: (Integral a) => a -> a correct 0 = 1 correct n = n * correct (n - 1) main :: IO () main = do putStrLn "Введите число для вычисления факториала: " input <- getLine let n = read input :: Int -- Convert the input from String to Int let xs = fac1 n let xy = fac2 n putStrLn $ "Вычисленный факториал 1 = " ++ show xs putStrLn $ "Вычисленный факториал 2 = " ++ show xyNow the program should work correctly. It reads the input as a
String, converts it to anInt, and then passes it to thefac1andfac2functions for calculation. The calculated factorials are then printed to the console.