简体   繁体   中英

Haskell - unification would give infinite type

I am new to Haskell and I just encountered this problem/error.

I have not a single clue what's going on, also I am not really familiar using lists and defining them as (l:k) ... i don't really know what l and k are considered... l is an element and k is a list?

Anyways, I would appreciate someone explained to me these l and k things or maybe (l:t:k) inside a function using list and maybe a way to write this simple delete function which, given that the element is inside the list, finds the first appearance of the desired element and deletes it, returning the new list.

    delete :: Eq b => b -> [b] -> [b]
    delete r (l:k)
        | inside r k = [l]:delete(r k)
        | otherwise = k

The pattern l:k does three things:

  1. It tells you the list is not empty if the pattern matches
  2. It binds l to the first element of the list
  3. It binds k to the rest of the list.

In the event you have a non-empty list, you want to compare your term r to l . If they are equal, you just return the rest of the list. Otherwise, you put l back on the list that results from the recursive call.

If the list is empty, you need to handle that as well, by just returning an empty list. (Deleting r from [] trivially produces [] .)

delete :: Eq b => b -> [b] -> [b]
delete r (h:t) | r == h = t   -- h for head, t for tail
               | otherwise = h : delete r t
delete _ [] = []   -- base case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM