Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does _ mean in Elm?

Tags:

elm

I'm looking at the zip example on http://elm-lang.org/examples/zip and I had a question about what exactly the _ means in Elm.

zip : List a -> List b -> List (a,b)
zip xs ys =
   case (xs, ys) of
    ( x :: xs', y :: ys' ) ->
        (x,y) :: zip xs' ys'

    (_, _) ->
        []

My hunch is that it means "everything else" but does that mean any valid value? What if there is no value?

like image 711
wmock Avatar asked Apr 14 '16 23:04

wmock


1 Answers

_ is used to match anything where you don't care about the value, so it's commonly used to match the "everything else" case.

In your example code (_, _) will match any tuple with 2 values in it. Note that it could also be replaced with just _ since you end up not caring about either value. A more illustrative example would be where you care about one value from the tuple but not the other, for example the implementation of fst in the core package

fst : (a,b) -> a
fst (a,_) =
  a

We don't care about the second value in the tuple, so it just matches with an _ in that position.

There is no null or undefined in Elm, so you don't have to worry about there being "no value" (if something would have no value, the Maybe type is used).

like image 111
robertjlooby Avatar answered Sep 28 '22 05:09

robertjlooby