我不确定这条错误消息是什么让我改变的,因为我看不到代码的问题,但显然有些错误,否则会编译。
错误讯息:
* Couldn't match expected type `(a, a)' with actual type `[(a, a)]'
* In the expression: xs ++ (flips xs)
In the expression: [xs ++ (flips xs)]
In an equation for `symClosure': symClosure xs = [xs ++ (flips xs)]
* Relevant bindings include
xs :: [(a, a)]
symClosure :: [(a, a)] -> [(a, a)]
symClosure xs = [xs ++ (flips xs)]
代码:
heads :: (Eq a) => [(a,a)] -> [a]
heads xs = [x | (x, _) <- xs]
tails :: (Eq a) => [(a,a)] -> [a]
tails xs = [x | (_,x) <- xs]
flips :: [a] -> [(a,a)]
flips xs = tails xs ++ heads xs
symClosure :: (Eq a) => [(a,a)] -> [(a,a)]
symClosure xs = [xs ++ (flips xs)]
旁注:我无法导入任何内容,也无法更改签名。
再一次,非常感谢任何帮助我理解的信息:)
答案 0 :(得分:2)
我认为您的flips
没有做出其签名所说的内容:
heads :: (Eq a) => [(a,a)] -> [a]
tails :: (Eq a) => [(a,a)] -> [a]
(++) :: [a] -> [a] -> [a]
换句话说,根据您的定义,此签名是正确的:
flips :: [(a, a)] -> [a]
flips xs = tails xs ++ heads xs
请注意,您只能在对名单上调用tails
和heads
。另请注意,您可以忽略上述所有签名中的Eq
约束。
如果您想要反转元组,可以使用zip代替
flips :: [(a, a)] -> [(a, a)]
flips xs = tails xs `zip` heads xs
至于symClosure
,取上面flips
的定义,表达式
symClosure xs = [xs ++ (flips xs)]
会产生一个包含单个元素的列表,它本身就是一对列表。这就解释了为什么它表示签名中的(a, a)
与表达式中的[(a, a)]
不匹配。您可能需要将括号括起来。
symClosure xs = xs ++ flips xs