2
votes

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. 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?

1
Pass in another argument to findInList. You're only giving it a list, not a thing to look for.nomen
Write less code :) Hint: use map, lookup, Data.List.catMaybes, and nub.chepner

1 Answers

1
votes

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. 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. aa is supposed to return a list, but findInList returns a Bool, so why do you need it at all?

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"