作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Haskell 中创建一个递归的合并排序函数。我被告知作为评估的一部分,我必须将类型定义为:
isort :: Ord a => [a] -> [a]
ord
是一个类。
Ord a
是什么意思?
isort :: Ord a => [a] -> [a]
isort [x] = [x]
isort (x:xs) = insert x (isort xs)
where
insert :: Int -> [Int] -> [Int]
insert a [] = []
insert a (b:c) | a < b = a:b:c
| otherwise = b : insert a c
Couldn't match type ‘a’ with ‘Int’
‘a’ is a rigid type variable bound by
the type signature for isort :: Ord a => [a] -> [a]
at LabSheet2.hs:17:10
Expected type: [a]
Actual type: [Int]
...
最佳答案
Ord a
是一个类型类约束,表明您的函数适用于任何类型 a
,只要 a
是可比的( Ord
erable )。您收到的错误消息是由于外部声明之间的冲突,表明它适用于任何 Ord a => a
,而内部 insert
“仅”适用于 Int
。
关于Haskell - Ord a 类型是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34023614/
我是一名优秀的程序员,十分优秀!