Is the pattern [_ | _] in Erlang something concrete?

I am learning Erlang from Teach you to Erlang , and I already saw the template [_|_] twice, but could not find any information in the topic. This use seems redundant, since excluding it from (1) and cutting it off with _ in (2) gives the same result without compromising readability. This is my first week with Erlang, so I may be completely wrong.

(1) from bestest_qsort :

 bestest_qsort(L=[_|_]) -> bestest_qsort(L, []). 

(2) from here :

  error:{badmatch,[_|_]} -> ok 
+6
source share
1 answer

Sample [p1 | p2] [p1 | p2] corresponds to a non-empty list whose head matches the pattern p1 and whose tail matches the pattern p2 . Since the pattern _ matches anything, [_ | _] [_ | _] matches any non-empty list.

_ By itself, on the other hand, matches anyone, including an empty list.

+11
source

Source: https://habr.com/ru/post/989461/


All Articles