【发布时间】:2013-01-08 20:13:13
【问题描述】:
我尝试编写一个函数,它接受子列表的列表,反转子列表并返回串联的反转子列表。这是我的尝试:
conrev :: Ord a => [[a]] -> [a]
conrev [[]] = []
conrev [[a]] = reverse [a]
conrev [(x:xs)] = reverse x ++ conrev [xs]
main = putStrLn (show (conrev [[1,2],[],[3,4]]))
我收到此错误:
3.hs:4:27:
Could not deduce (a ~ [a])
from the context (Ord a)
bound by the type signature for conrev :: Ord a => [[a]] -> [a]
at 3.hs:1:11-31
`a' is a rigid type variable bound by
the type signature for conrev :: Ord a => [[a]] -> [a] at 3.hs:1:11
In the first argument of `reverse', namely `x'
In the first argument of `(++)', namely `reverse x'
In the expression: reverse x ++ conrev [xs]
我做错了什么?第二个问题是 - 类型签名可以更通用吗?我必须尽可能写得通俗一点。
【问题讨论】:
-
据我所知,你不需要 Ord a =>
-
这边:
conrev :: [[]] -> []?我收到一个错误,Expecting one more argument to '[]' -
尝试 conrev :: [[a]] -> [a]
-
Couldn't match expected type 'a' with actual type '[a]' 'a' is a rigid type variable bound by the type signature for conrev :: [[a]] -> [a] at 3.hs:1:11
标签: list haskell reverse signature type-deduction