F# Tip Of The Week (Week of August 12, 2013)

The F# developer can use pattern matching syntax pretty much anywhere in F#.  For example, consider the following list:


let l = [1..25]

If I want to get the last element of the list, I can do this with a little trivial pattern match:


let lastElem::_ = l |> List.rev;;

stdin(7,5): warning FS0025: Incomplete pattern matches on this expression. For example, the value '[]' may indicate a case not covered by the pattern(s).

val lastElem : int = 25

In this case, you can safely ignore the warning.

You can also pull one of the elements from a tuple very easily with this pattern match:


let name,_ = (“Onorio”,”11W”);;

val name : string = "Onorio"

The underscore character in both of these pattern matches functions as a wildcard.  In both cases it signifies a value that we don’t care about.

2 responses

  1. Pingback: F# Weekly #33 2013 | Sergey Tihon's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: