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?
_
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With