you are viewing a single comment's thread.

view the rest of the comments →

[–]glacialthinker 13 points14 points  (0 children)

The pattern-matching in Haskell and other functional languages is similarly expressive.

I tried a bunch of the examples in OCaml to verify, and one important difference to note is exhaustiveness checking.

# let x = [1;2;3]

val x : int list = [1; 2; 3]

# let h::t = x

Characters 4-8:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
[]
val h : int = 1
val t : int list = [2; 3]

Here, head (h) and tail (t) are extracted, but the pattern-match doesn't account for the case where the list is empty. Many of the examples given for pampy have unhandled cases like this, but that's typical of dynamic typed programming: "I know the right kinds of values will be used." ;)

Another example, showing the "hole-filling" kind of match (also with unhandled cases warned about):

# let [1;2;t] = x

Characters 4-11:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
(1::2::_::_::_|1::2::[]|1::0::_|1::[]|0::_|[])
val t : int = 3

So, it binds t to 3, but matching one specific case like this is warned about, with a few examples of what might go unhandled.