簡體   English   中英

haskell 無法將預期類型與實際類型“Bool”匹配

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM