基本上,我尝试编写一个包含两个列表的程序:x = [x1,x2,..,xn]和[[y1,z1),...,(yn,zn)],在输出中应该如果可以在第一个列表中找到相应的y1 ... yn,则打印仅包含这些z1 ... zn值的列表。有我的代码:
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,y]
aa2 = aa ['v', 'a', 'i', 'i', 'y'] [('v','h'), ('a', 'e'), ('i', 'l'), ('y','o'), ('h','y')] --output should be [h,e,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"