gpt4 book ai didi

design-patterns - Haskell 错误的 IO 输出

转载 作者:行者123 更新时间:2023-12-04 05:10:48 25 4
gpt4 key购买 nike

这就是我所拥有的,我想要这样的输出

*Main> sumThree
Please enter three integers:
1
2
3
Their sum is: 6
(1,2,3)

然而,我不断得到,没有总和
*Main> sumThree
Please enter three integers:
1
2
3
Their sum is:
(1,2,3)
**summ :: Integer -> Integer -> Integer -> IO Integer
summ x1 x2 x3 = return(x1+x2+x3)
sumThree :: IO (Integer, Integer, Integer)
sumThree = do putStr "Please enter tree integers:"
x1 <- getInteger
x2 <- getInteger
x3 <- getInteger
putStr "Their Sum is: "
summ x1 x2 x3
return(x1,x2,x3)**

最佳答案

您必须实际打印 summ 的结果.

不要return在您的 summ函数,所以函数的结果类型是 Integer而不是 IO Integer .然后,做

putStr (show (summ x1 x2 x3))

而不仅仅是 summ x1 x2 x3 .

此外,您确实了解 sum内置(Prelude)功能吧?它需要一个数组 Num的,所以你可以做 putStr (show (sum [x1, x2, x3])) .

关于design-patterns - Haskell 错误的 IO 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14948477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com