【发布时间】:2016-12-19 12:23:17
【问题描述】:
我正在阅读“Haskell 编程”,我正在尝试合并两个排序列表。这是我的代码:
mymerge :: Ord a => [a] -> [a] -> [a]
mymerge xs [] = xs
mymerge [] ys = ys
mymerge (x:xs) (y:ys) | x < y = x : mymerge xs (y:ys)
| otherwise = y : mymerge (x:xs) ys
它适用于所有情况,除非我尝试将测试定义为:
t72 = mymerge [] []
错误是:
No instance for (Ord a0) arising from a use of ‘mymerge’
The type variable ‘a0’ is ambiguous
Relevant bindings include t72 :: [a0] (bound at ch06.hs:109:1)
Note: there are several potential instances:
instance (Ord a, Ord b) => Ord (Either a b)
-- Defined in ‘Data.Either’
instance forall (k :: BOX) (s :: k). Ord (Data.Proxy.Proxy s)
-- Defined in ‘Data.Proxy’
instance (GHC.Arr.Ix i, Ord e) => Ord (GHC.Arr.Array i e)
-- Defined in ‘GHC.Arr’
...plus 26 others
In the expression: mymerge [] []
In an equation for ‘t72’: t72 = mymerge [] []
我怀疑它试图告诉我没有足够的信息来推断 [] 的类型。如果我明确定义函数的类型,它会起作用:
t72 :: Ord a => [a]
t72 = mymerge [] []
这是让它工作的惯用方式吗?
更新:我不会按照建议将其标记为重复,另一个问题的答案没有提到单态限制。
【问题讨论】:
-
你用的是什么版本?
-
听起来你理解这个问题。你通常会给参数一个具体的类型,比如
merge ([] :: [Int]) [] -
@karafka,我有 GHC 7.10.3
-
@jberryman,我对 Haskell 了解的不够多,无法判断这是否与您提到的问题重复 :-)
标签: haskell