News for hackers. Stuff that matters.
Re:How to stop functional programming by Jtsummers on Tuesday February 23, 2016 @12:21PM (#11160491)
In other languages with pattern matching you often have some sort of wildcard that you can match against. Either an explicitly named variable or, commonly, an underscore (`_`) if the value is unused. Example in erlang:

  foo(bar) -> baz;
  foo(X)   -> bang.
The X is an explicit variable that will match anything not already matched in previous patterns. It can also be used. (NB: Erlang pattern matching is the same or essentially the same for this discussion at each possible level, whether in a function declaration as above or a case statement or when receiving messages.)

Here, we don't care about the thing we've named with an underscore:

  tail([_|Tail]) -> Tail.
In Erlang, `[1|[2,3]]` is equivalent to the list `[1,2,3]`. So `[H|T]` as a pattern would have `H = 1` and `T = [2,3]`. Since we don't care about the head of the list, we name it with _. Optionally, in Erlang, you can give it a fuller name:

  tail([_Head|Tail]) -> Tail.
The compiler knows that _Head is supposed to be unused and won't complain (warning about unused variables).

  tail([Head|Tail]) -> Tail.
Would give a warning.

There are no comments on this comment yet.