我只是在学习haskell(我自己,为了好玩)而且我碰壁了。
我的问题:
如何定义函数
flrt = (floor . sqrt)
当我在文件中尝试并编译时,GCHi 抱怨以下内容:
AKS.hs:11:9:
No instance for (RealFrac Integer)
arising from a use of `floor'
Possible fix: add an instance declaration for (RealFrac Integer)
In the first argument of `(.)', namely `floor'
In the expression: (floor . sqrt)
In an equation for `flrt': flrt = (floor . sqrt)
AKS.hs:11:17:
No instance for (Floating Integer)
arising from a use of `sqrt'
Possible fix: add an instance declaration for (Floating Integer)
In the second argument of `(.)', namely `sqrt'
In the expression: (floor . sqrt)
In an equation for `flrt': flrt = (floor . sqrt)
我不明白为什么生成的函数不仅仅是 Int -> Int。
我刚刚完成了 CS 的第二年并完成了基础 PL 课程。我听说过,但还不太了解类型。我尝试阅读了一些 haskell 教程,但这一切都超出了我的想象。
PS - 我也不明白什么是单子。(我的搜索出现的许多其他问题都谈到了这些)
PPS - 我的完整来源
bar = \a b -> if (2^a) > b
then (a-1)
else bar (a+1) b
foo = bar 1
flrt :: Integer -> Integer
flrt = (floor . sqrt)
aks target = if (target < 2)
then putStr "Not a Prime.\n\n"
else if elem (mod target 10) [0,2,4,5,6,8]
then putStr "Composite\n\n"
else if (elem target) [a^b | a <- [3,5..(flrt target)], b <- [1.. (foo target)]]
then putStr "Composite\n\n"--}
else
putStr "filler"