[英]haskell couldn't match expected type with actual type 'Bool'
基本上我尝试编写一个程序,它有两个列表:x=[x1,x2,..,xn] 和 [(y1,z1),...,(yn,zn)] 并且在输出中它应该打印一个仅包含那些 z1...zn 值的列表,如果在第一个列表中可以找到相应的 y1...yn。 有我的代码:
merge x [] = x
merge [] y = y
merge (head1:tail1) (head2:tail2) = head1 : head2 : merge tail1 tail2
removeDuplicates (x:xs) =
let a =
if findInList x xs == True then []
else [x] in a ++ removeDuplicates xs
findInList x [] = False
findInList x (y:ys) = if x == y then True else findInList x ys
kk [] _ = []
kk _ [] = []
kk x y = removeDuplicates( kk_optimize x y)
kk_optimize [] _ = []
kk_optimize _ [] = []
kk_optimize (head1:tail1) (head2:tail2) = merge( kk_sub head1 (head2:tail2)) (kk_optimize tail1 (head2:tail2))
kk_sub _ [] =[]
kk_sub head1 (head2:tail2) = merge (if snd(head1)==fst(head2) then [(fst(head1),snd(head2))] else []) ( kk_sub head1 tail2)
aa [] dictionary = []
aa text dictionary = findInList ((subAa text dictionary) ++ (aa (tail text) dictionary))
subAa text [] = []
subAa [] dictionary = []
subAa text dictionary =
if (head text) == fst (head dictionary)
then snd (head dictionary) : subAa text(tail dictionary)
else subAa text (tail dictionary)
aa1 = aa ['h', 'e', 'l', 'l', 'o'] [('h', 'w'), ('e', 'o'), ('l','r'), ('o','y'), ('r', 't')] --output should be [w,o,r,r,y]
aa2 = aa ['v', 'a', 'i', 'i', 'y'] [('v','h'), ('a', 'e'), ('i', 'l'), ('y','o'), ('h','y')] --output should be [h,e,l,l,o]
但是,当我尝试编译它时,出现错误:
main.hs:29:26: error:
• Couldn't match expected type ‘[a1]’
with actual type ‘[[a1]] -> Bool’
• Probable cause: ‘findInList’ is applied to too few arguments
In the expression:
findInList ((subAa text dictionary) ++ (aa (tail text) dictionary))
In an equation for ‘aa’:
aa text dictionary
= findInList
((subAa text dictionary) ++ (aa (tail text) dictionary))
• Relevant bindings include
dictionary :: [(a, a1)] (bound at main.hs:29:13)
aa :: [a] -> [(a, a1)] -> [a1] (bound at main.hs:27:5)
|
29 | aa text dictionary = findInList ((subAa text dictionary) ++ (aa (tail text) dictionary))
我怎么能解决这个问题?
请注意以下几行:
• Probable cause: ‘findInList’ is applied to too few arguments
In the expression:
findInList ((subAa text dictionary) ++ (aa (tail text) dictionary))
函数findInList
有两个参数。 在显示的表达式中,您已经给了它一个。 具体来说,看起来你给了它列表,但没有告诉它在列表中找到什么。 但是,我完全不知道您为什么在这里使用findInList
。 aa
应该返回一个列表,但findInList
返回一个Bool
,那么你为什么需要它呢?
我想你只是想要:
aa text dictionary = (subAa text dictionary) ++ (aa (tail text) dictionary)
更改后,程序类型检查并似乎几乎可以执行您想要的操作:
> aa1
"worry"
> aa2
"hello"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.