gpt4 book ai didi

haskell - 为什么我会因使用 show 错误而得到 No (Show t0) 实例?

转载 作者:行者123 更新时间:2023-12-02 21:29:37 29 4
gpt4 key购买 nike

我陷入了这个问题,而且我对 Haskell 很陌生,我试图用以下代码完成第一个欧拉问题:

main = putStrLn . show . sum $ [3,6..1000]:[5,10..1000]

这是错误的第一部分:

euler/1.hs:1:19:
No instance for (Show t0) arising from a use of `show'
The type variable `t0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Show Double -- Defined in `GHC.Float'
instance Show Float -- Defined in `GHC.Float'
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus 23 others
In the first argument of `(.)', namely `show'
In the second argument of `(.)', namely `show . sum'
In the expression: putStrLn . show . sum

最佳答案

您希望[3,6..1000]:[5,10..1000]做什么? x : xs 将一个对象准备到对象列表中。这里两个参数都是整数列表。您想要 ++ 来代替(串联)吗?

需要指出的是,无论您期望 : 做什么,您的方法都是不对的。如果您想自己弄清楚的话,我不会对此进行详细说明。

关于haskell - 为什么我会因使用 show 错误而得到 No (Show t0) 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037417/

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