module myModule =
let rec consecutiveNumberDetector (input:list) =
match input with
| 2::t when t.Length>2 && (List.head t)=3 && (t |> List.tail |> List.head) =4 ->
"* got 2,3,4".Dump()
consecutiveNumberDetector (t |> List.tail |> List.tail)
| h::t ->
String.Concat("* head is: " , h, " and tail is:", t).Dump()
consecutiveNumberDetector t
| [] -> "* Empty list".Dump();
let myList = [1;2;3;5;2;3;4;99]
myModule.consecutiveNumberDetector myList
Resulting output is:
* head is: 1 and tail is:[2; 3; 5; ... ] * head is: 2 and tail is:[3; 5; 2; ... ] * head is: 3 and tail is:[5; 2; 3; ... ] * head is: 5 and tail is:[2; 3; 4; ... ] * got 2,3,4 * head is: 99 and tail is:[] * Empty list
Download the linqpad file here
See also more about pattern matching at
https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/match-expressions
No comments:
Post a Comment