我是 Haskell 的新手,正在阅读 Haskell from first principles .
现在我在看第 5 章。 在解决它的练习时,特别是 7,8 我不明白为什么我没有想出正确的答案
问题来了
你可以找到问题和解决方案here
If the type of kessel is (Ord a, Num b) => a -> b -> a, then the type of kessel 1 2 is:
- Integer
- Int
- a
- (Num a, Ord a) => a
- Ord a => a
- Num a => a
我认为它的答案是5。 Ord a => a
,bcs 我想出的一种可能实现是完全忽略参数 b
像这样
kessel ::(Ord a, Num b) => a -> b -> a
kessel a b = a
--if u have any other implementation Please share
因为它完全忽略了 b
它不应该影响 a
的类型但是 ghci 仍然显示它的类型是
:t kessel 1 2
kessel 1 2 :: (Ord a, Num a) => a
我错过了什么? Q:8 也是如此
最佳答案
Num b
是一条红鲱鱼。 Num a
约束是因为您为 a
类型传递了一个数字文字,而那些需要 Num
类型类。尝试 :t kessel [] 2
让它消失。
关于Haskell 来自 First Principles Ch5, Ex 7,8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67168490/