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.
  1. //use F# library  
  2. open System  
  3. //change the CLI title  
  4. System.Console.Title<-"Display Military Phoenitic Alphabet"  
  5. //adds color to our console application  
  6. System.Console.ForegroundColor<-ConsoleColor.Blue  
  7. //asks the user to enter a letter  
  8. printfn "Enter a letter:"  
  9. //convert the input to character and convert it to uppercase letter  
  10. //this is just for comparison purpose  
  11. let chrletter=Char.ToUpper(Convert.ToChar(System.Console.ReadLine()))  
  12. //clear the screen     
  13. System.Console.Clear()  
  14. //match the value of chrletter to the ff. values  
  15. match chrletter with  
  16. //if the value of chrletter is a or A display Alpha  
  17. 'A' ->printfn "Alpha"  
  18. //if the value of chrletter is b or B display Bravo  
  19. 'B' ->printfn "Bravo"  
  20. //if the value of chrletter is c or C display Charlie  
  21. 'C' ->printfn "Charlie"  
  22. //if the value of chrletter is d or D display Delta  
  23. 'D' ->printfn "Delta"  
  24. //if the value of chrletter is e or E display Echo  
  25. 'E'->printfn "Echo"  
  26. //if the value of chrletter is f or F display Foxtrot  
  27. 'F'->printfn "FoxTrot"  
  28. //if the value of chrletter is g or G display Golf  
  29. 'G'->printfn "Golf"  
  30. //if the value of chrletter is h or H display Hotel  
  31. 'H'->printfn "Hotel"  
  32. //if the value of chrletter is i or I display India  
  33. 'I'->printfn "India"  
  34. //if the value of chrletter is j or J display Juliet  
  35. 'J'->printfn "Juliet"  
  36. //if the value of chrletter is k or K display Kilo  
  37. 'K'->printfn "Kilo"  
  38. //if the value of chrletter is l or L display Lima  
  39. 'L'->printfn "Lima"  
  40. //if the value of chrletter m or M display Mike  
  41. 'M'->printfn "Mike"  
  42. //if the value of chrletter is n or N display November  
  43. 'N'->printfn "November"  
  44. //if the value of chrletter is o or O display Oscar  
  45. 'O'->printfn "Oscar"  
  46. //if the value of chrletter is p or P display Papa  
  47. 'P'->printfn "Papa"  
  48. //if the value of chrletter is q or Q display Quebec  
  49. 'Q'->printfn "Quebec"  
  50. //if the value of chrletter is r or R display Romeo  
  51. 'R'->printfn "Romeo"  
  52. //if the value of chrletter is s or S display Sierra  
  53. 'S'->printfn "Sierra"  
  54. //if the value of chrletter is t or T display Tango  
  55. 'T'->printfn "Tango"  
  56. //if the value of chrletter is u or U display Uniform  
  57. 'U'->printfn "Uniform"  
  58. //if the value of chrletter is v or V display Victor  
  59. 'V'->printfn "Victor"  
  60. //if the value of chrletter is w or W display Whiskey  
  61. 'W'->printfn "Whiskey"  
  62. //if the value of chrletter is x or X display X-Ray  
  63. 'X'->printfn "X-Ray"  
  64. //if the value of chrletter is y or Y display Yankee  
  65. 'Y'->printfn "Yankee"  
  66. //if the value of chrletter is z or Z display Zulu  
  67. 'Z'->printfn "Zulu"  
  68. //otherwise  
  69. | _ ->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.

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

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