Visual F# 100 Examples: Example 14 and 15(Pattern Matching)

Pattern matching/match with statement is similar to the switch selection statement in other programming languages. The following examples demonstrate how to use it:


Problem: Make an application that will asks a letter and displays its equivalent U. S. military phonetic alphabet.
//use F# library
open System
//change the CLI title
System.Console.Title<-"Display Military Phoenitic Alphabet"
//adds color to our console application
System.Console.ForegroundColor<-ConsoleColor.Blue
//asks the user to enter a letter
printfn "Enter a letter:"
//convert the input to character and convert it to uppercase letter
//this is just for comparison purpose
let chrletter=Char.ToUpper(Convert.ToChar(System.Console.ReadLine()))
//clear the screen   
System.Console.Clear()
//match the value of chrletter to the ff. values
match chrletter with
//if the value of chrletter is a or A display Alpha
| 'A' ->printfn "Alpha"
//if the value of chrletter is b or B display Bravo
| 'B' ->printfn "Bravo"
//if the value of chrletter is c or C display Charlie
| 'C' ->printfn "Charlie"
//if the value of chrletter is d or D display Delta
| 'D' ->printfn "Delta"
//if the value of chrletter is e or E display Echo
| 'E'->printfn "Echo"
//if the value of chrletter is f or F display Foxtrot
| 'F'->printfn "FoxTrot"
//if the value of chrletter is g or G display Golf
| 'G'->printfn "Golf"
//if the value of chrletter is h or H display Hotel
| 'H'->printfn "Hotel"
//if the value of chrletter is i or I display India
| 'I'->printfn "India"
//if the value of chrletter is j or J display Juliet
| 'J'->printfn "Juliet"
//if the value of chrletter is k or K display Kilo
| 'K'->printfn "Kilo"
//if the value of chrletter is l or L display Lima
| 'L'->printfn "Lima"
//if the value of chrletter m or M display Mike
| 'M'->printfn "Mike"
//if the value of chrletter is n or N display November
| 'N'->printfn "November"
//if the value of chrletter is o or O display Oscar
| 'O'->printfn "Oscar"
//if the value of chrletter is p or P display Papa
| 'P'->printfn "Papa"
//if the value of chrletter is q or Q display Quebec
| 'Q'->printfn "Quebec"
//if the value of chrletter is r or R display Romeo
| 'R'->printfn "Romeo"
//if the value of chrletter is s or S display Sierra
| 'S'->printfn "Sierra"
//if the value of chrletter is t or T display Tango
| 'T'->printfn "Tango"
//if the value of chrletter is u or U display Uniform
| 'U'->printfn "Uniform"
//if the value of chrletter is v or V display Victor
| 'V'->printfn "Victor"
//if the value of chrletter is w or W display Whiskey
| 'W'->printfn "Whiskey"
//if the value of chrletter is x or X display X-Ray
| 'X'->printfn "X-Ray"
//if the value of chrletter is y or Y display Yankee
| 'Y'->printfn "Yankee"
//if the value of chrletter is z or Z display Zulu
| 'Z'->printfn "Zulu"
//otherwise
| _ ->printfn "Invalid input"
The last statement |_ is similar to the
default statement in other programming languages Switch conditional structure. It is automatically executed when no pattern match is found. Don't forget to add it at the end of every match with statement otherwise you will get an “Incomplete pattern matches on this expression” error.


Problem: Make an application that will ask the month number and display the corresponding month name and the number of days in it. Use pattern matching.

//use F# library
open System
//change the CLI title
System.Console.Title<-"Display Month Name"
//adds color to our console application
System.Console.ForegroundColor<-ConsoleColor.Blue
System.Console.BackgroundColor<-ConsoleColor.White
//asks the user to enter a month number
printfn "Enter a month number(1-12):"
let intmonth=Convert.ToInt32(System.Console.ReadLine())
//clear the screen   
System.Console.Clear()
//match the value of intmonth to the ff. values
match intmonth with
//if the value of intmonth is 1 display January
| 1 ->printfn "January(31 days)"
//if the value of intmonth is 2 display February
| 2 ->printfn "Febrary(28/29 days)"
//if the value of intmonth is 3 display March 
| 3 ->printfn "March(31 days)"
//if the value of intmonth is 4 display April
| 4 ->printfn "April(30 days)"
//if the value of intmonth is 5 display May
| 5 ->printfn "May(31 days)"
//if the value of intmonth is 6 display June
| 6 ->printfn "June(30 days)"
//if the value of intmonth is 7 display July
| 7 ->printfn "July(31 days)"
//if the value of intmonth is 8 display August
| 8 ->printfn "August(31 days)"
//if the value of intmonth is 9 display September
| 9 ->printfn "September(30 days)"
//if the value of intmonth is 10 display October
| 10 ->printfn "October(31 days)"
//if the value of intmonth is 11 display November
| 11 ->printfn "November(30 days)"
//if the value of intmonth is 12 display December
| 12->printfn "December(31 days)"
//otherwise
| _ ->printfn "Invalid input"

That's all for now my Visual F# friends. Ciao!

Visual F# 100 Examples: Example 12 and 13

This is the continuation of our planned 100 Visual F# examples. By the way, the bugs in examples number 7 to 11 had been fixed. My apologies, I should’nt have written those without using Visual F# compiler:)


Problem: Make a console application that will asks the user to enter the day that he was born and display his character or future based on the famous nursery rhyme written in England, “Monday’s Child”.

open System
//changes the console application title
System.Console.Title<-"Tell the Future"
//adds foreground color
System.Console.ForegroundColor<-ConsoleColor.Cyan
printfn "What day were you born?"
let strday=System.Console.ReadLine()
//clears the screen
System.Console.Clear()
//converts the inputs to lowercase then compare it our
//specified values
if strday.ToLower()="monday" then
    printfn "Monday's child is fair of face"
else if strday.ToLower()="tuesday" then
    printfn "Tuesday's child is full of grace"
else if strday.ToLower()="wednesday" then
    printfn "Wednesday's child is full of woe"
else if strday.ToLower()="thursday" then
    printfn "Thurdays's child has far to go"
else if strday.ToLower()="friday" then
    printfn "Friday's child is loving and giving"
else if strday.ToLower()="saturday" then
    printfn "Saturday's child works hard for a living"
else if strday.ToLower()="sunday" then
    printfn "Sunday's child is bonny and blithe and good and gay"
else
    printfn "Invalid input"
Problem: Develop a console application that will asks the wind speed in kilometer per hour(kph) and display its equivalent Philippine Storm Signal number.
open System
//changes the console application title
System.Console.Title<-"Determine Storm Signal Number"
//adds foreground color
System.Console.ForegroundColor<-ConsoleColor.Cyan
printfn "Enter wind speed(kph):"
let intspeed=Convert.ToInt32(System.Console.ReadLine())
//clears the screen
System.Console.Clear()
//if the wind speed ranges from 30 to 60
if intspeed>=30 && intspeed<=60 then
    printfn "Storm signal number 1"
//if the wind speed ranges from 61 to 100
else if intspeed>60 && intspeed<=100 then
    printfn "Storm signal number 2"
//if the wind speed ranges from 101 to 185
else if intspeed>100 && intspeed<=185 then
    printfn "Storm signal number 3"
//if the wind speed is greater than 185
else if intspeed>185 then
printfn "Storm signal number 4"
else 
printfn "Invalid input"