我正在学习Haskell和我试图实现我自己的replicate
功能,下面是我的工作的结果是:通过列表理解来实现复制功能
replicate' :: Enum a => a -> b -> [b]
replicate' a b = [b | _ <- [1..a]]
但在加载脚本到ghci中我得到msg:
proginhaskell.hs:152:29:
Could not deduce (Num a) arising from the literal `1'
from the context (Enum a)
bound by the type signature for
replicate' :: Enum a => a -> b -> [b]
at proginhaskell.hs:152:1-34
Possible fix:
add (Num a) to the context of
the type signature for replicate' :: Enum a => a -> b -> [b]
In the expression: 1
In the expression: [1 .. a]
In a stmt of a list comprehension: _ <- [1 .. a]
我想问一下如何修复这个问题。这是否是正确的方法来实现复制功能?
“至于这段代码有多好,我可能会用明确的递归来写它,以避免生成一个无用的列表,但都是很好的实现。” - 我相信GHC能够优化这种情况,并且摆脱'[1..a]'生成的列表。参见[快捷融合](http://www.haskell.org/haskellwiki/Short_cut_fusion)。 –
@PedroRodrigues我很好奇,分析了核心,看起来两者都被编译成相同的代码,最后我更喜欢前奏'take'。我相应地编辑了我的答案 – jozefg