【发布时间】:2016-04-28 11:25:09
【问题描述】:
Haskell "No instance for" error
Haskell Error: "No instance for (Enum [Int])
Function definition problems (No instance for … arising from)
No instance for Num String arising from the literal `1'
我是 Haskell 的新手,一般来说是函数式编程。此外,我知道上述问题非常相似,但我无法在任何地方找到解决我的问题的方法。以下代码用于查找数字输入的因数:
fc4 :: Double -> IO ()
check :: Double -> Double -> IO ()
fc4 a = check a (sqrt a)
check a b = if rem a b == 0 then print (b, div a b) else print () >> if b > 1 then check a (b-1) else putStrLn ("Done.")
我尝试从Double 切换到Integer 并返回所有可能的组合,但每个组合都失败并出现相同的错误:
No instance for (Integral Double) arising from a use of 'rem'
我还尝试在rem 的参数上显式使用fromIntegral 和toInteger,但我尝试的任何组合都没有避免这个问题。我还从文档中看到rem 的类型是Integral a => a -> a -> a,因此似乎对a 和b 使用相同的类型总是有效的,无论是Double 还是Integer。
我在做某事吗?我犯了一些可怕的菜鸟错误吗?作为参考,here 是我希望实现的 C++ 版本。提前致谢!
【问题讨论】:
标签: haskell