0

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
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
White_Sirilo
  • 264
  • 1
  • 11
  • 1
    I think it probably would help a lot if *before* writing list processing algorithms, you have a basic understanding of how lists are represented. You have a nice article here http://learnyouahaskell.com/starting-out Sure you do not need to have an in-depth understanding into how the Haskell ccompiler works, or how category theory is related to Haskell, etc. but it usually helps to have some insight first. – Willem Van Onsem Apr 06 '18 at 12:34
  • 2
    There are here two things wrong: you do not process the base case `[]` (the empty list, and furthermore `[l] : delete (r k)` does not makes much sense since `l` is an element, you probably want to rewrite that to `l : delete r k`. – Willem Van Onsem Apr 06 '18 at 12:35
  • 1
    You want to compare `r` and `l`, not ignore `l` and look for `r` inside `k`. – chepner Apr 06 '18 at 12:39
  • 1
    @WillemVanOnsem One more variation on the [*Why doesn't this function work if I use “\[xs\]” instead of “xs”?*](https://stackoverflow.com/q/48884276/2751851) theme. – duplode Apr 06 '18 at 12:55
  • Thank you guys, i appreciate it. I will check learnyouahaskell.com later. – White_Sirilo Apr 06 '18 at 12:59
  • @duplode: indeed, and it is rather sad that actually every time lack of knowledge about lists is actually the reason why the person could not implement it himself/herself. – Willem Van Onsem Apr 06 '18 at 12:59
  • `delete(r k)` means: call function `r` with single argument `k`; take its result, and call `delete` with single argument (previous result). Surely you do want this. To call `delete` with two arguments use `delete r k`. – chi Apr 06 '18 at 16:51

1 Answers1

2

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • i think your otherwise is faulty, did you mean: otherwise = h:delete r t ? Thanks for the help tho, i think i made it. – White_Sirilo Apr 06 '18 at 12:57