6

F# Tip Of The Week (Week of August 12, 2013) « Onor.io

 3 years ago
source link: https://onor.io/2013/08/12/f-tip-of-the-week/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK