简体   繁体   English

haskell 无法将预期类型与实际类型“Bool”匹配

[英]haskell couldn't match expected type with actual type 'Bool'

Basically i try to write a program which has two lists: x=[x1,x2,..,xn] and [(y1,z1),...,(yn,zn)] and in the output it should print a list that contains only those z1...zn values, if corresponding y1...yn could be found in the first list.基本上我尝试编写一个程序,它有两个列表:x=[x1,x2,..,xn] 和 [(y1,z1),...,(yn,zn)] 并且在输出中它应该打印一个仅包含那些 z1...zn 值的列表,如果在第一个列表中可以找到相应的 y1...yn。 There is my code:有我的代码:

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]

However when i try to compile it i get an error:但是,当我尝试编译它时,出现错误:

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)) 

How could i fix this?我怎么能解决这个问题?

Note the lines:请注意以下几行:

• Probable cause: ‘findInList’ is applied to too few arguments
  In the expression:
    findInList ((subAa text dictionary) ++ (aa (tail text) dictionary))

The function findInList takes two arguments.函数findInList有两个参数。 In the expression shown, you have given it one.在显示的表达式中,您已经给了它一个。 Specifically, it looks like you gave it the list, but didn't tell it what to find in the list.具体来说,看起来你给了它列表,但没有告诉它在列表中找到什么。 However, I can't tell why you're using findInList here at all.但是,我完全不知道您为什么在这里使用findInList aa is supposed to return a list, but findInList returns a Bool , so why do you need it at all? aa应该返回一个列表,但findInList返回一个Bool ,那么你为什么需要它呢?

I think you just want:我想你只是想要:

aa text dictionary = (subAa text dictionary) ++ (aa (tail text) dictionary)

After that change, the program type checks and seems to do almost what you want:更改后,程序类型检查并似乎几乎可以执行您想要的操作:

> aa1
"worry"
> aa2
"hello"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将预期类型`Bool'与实际类型`IO Bool'匹配 - Couldn't match expected type `Bool' with actual type `IO Bool' 无法将预期类型`Bool'与实际类型`Card - > Bool'匹配 - Couldn't match expected type `Bool' with actual type `Card -> Bool' 无法将预期类型“Bool”与实际类型“Char -> Bool”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘Char -> Bool´ 无法将预期类型“Bool”与实际类型“a -> Bool”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘a -> Bool’ [HASKELL]无法将预期类型`[a0]'与实际类型`[a1] - > Bool'匹配 - [HASKELL]Couldn't match expected type `[a0]' with actual type `[a1] -> Bool' Haskell:无法将预期类型'IO b'与实际类型bool匹配 - Haskell: Couldn't match expected type 'IO b' with actual type bool 无法将预期类型'Integer - > t'与实际类型'Bool'匹配 - Couldn't match expected type ‘Integer -> t’ with actual type ‘Bool’ 无法将预期类型“Bool”与实际类型“[[Integer]] 匹配 - Couldn't match expected type `Bool' with actual type `[[Integer]] 无法将预期类型“布尔”与实际类型“ Int”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘Int’ 无法将预期的类型“布尔”与实际类型“(a,a)”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘(a, a)’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM