4 ms·
He is right in that order is important when pattern matching. However, his premise - an empty list has a head and a tail - is incorrect, so in this case order
by GrinningFool 13y ago
He is right in that order is important when pattern matching. However, his premise - an empty list has a head and a tail - is incorrect, so in this case order does not matter:
> [H] = [].
** exception error: no match of right hand side value []
> [H|T] = [].
** exception error: no match of right hand side value []
But when it comes to pattern matching, order is important:
> Test = fun(Value) -> Value ; (hello) -> world end.
> Test(hello).
hello
As opposed to:
> Test2 = fun(hello) -> world ; (Value) -> Value end.
> Test2(hello).
world
---
For the sake of completeness it's worth mentioning that a list with a single item has both a head and a tail:
> [H|T] = [1].
[1]
> H.
1
> T.
[]
(Which is why you need to have a function head that matches [], whether first or last in his example.)