1

I have stumbled across an error that was very confusing to me. I had a recursive function with the pattern matching ["",_] = [] which simply didnt work. After many times of trying to find my mistake I tried switching it up with ("":_) = []. And it worked! I just want to know why the first term didnt work but the second one did. This function works and the pattern matching is called:

takeAllElem :: [String] -> [String]
takeAllElem ("":_) = []
takeAllElem (xs) =  map takeFirstElem (xs) :  (takeAllElem (removeFirstElem (xs)))

This function also works but the pattern matching is never called:

takeAllElem :: [String] -> [String]
takeAllElem ["",_] = []
takeAllElem (xs) =  map takeFirstElem (xs) :  (takeAllElem (removeFirstElem (xs)))

Is this just a syntax error or does the compiler interpret ["",_] differently to ("":_).

chepner
  • 497,756
  • 71
  • 530
  • 681
FarbKlexx
  • 31
  • 3

1 Answers1

8

["", _] is a list containing exactly two elements, equivalent to "":_:[].

("":_) is any non-empty list whose first element is the empty string. The tail is an arbitrary list.

chepner
  • 497,756
  • 71
  • 530
  • 681