我正在尝试使用unfoldr
在Haskell中实现选择排序,但我的函数不断抛出类型错误,我不确定如何解决它。
我的选择代码排序:
unfoldSort = unfoldr select
select [] = Nothing
select xs = Just (m, delete m xs)
where m = minimum xs
这是我收到的错误消息:
• Ambiguous type variable ‘a0’ arising from a use of ‘select’
prevents the constraint ‘(Ord a0)’ from being solved.
Relevant bindings include
unfoldSort :: [a0] -> [a0]
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance (Ord b, Ord a) => Ord (Either a b)
-- Defined in ‘Data.Either’
instance Ord Ordering -- Defined in ‘GHC.Classes’
instance Ord Integer
-- Defined in ‘integer-gmp-1.0.0.1:GHC.Integer.Type’
...plus 23 others
...plus 38 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘unfoldr’, namely ‘select’
In the expression: unfoldr select
In an equation for ‘unfoldSort’: unfoldSort = unfoldr select
答案 0 :(得分:2)
当您说:{/ 1>}时,unfoldSort
会返回Maybe
。
unfoldSort :: t -> Maybe (t, t)
但这是真的吗?请注意,unfoldSort
的返回类型必须与unfoldr
的返回类型相同,因为您只是返回它给您的任何内容。
unfoldr
有签名:
unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
请注意,它会返回一个列表,而不是Maybe
,这意味着您的unfoldSort
也必须返回一个列表。
更改签名,使其返回列表,而不是Maybe
。