我想了解活動模式,所以我用FizzBuzz玩弄:FizzBuzz與活動模式
let (|Fizz|_|) i = if i % 3 = 0 then Some Fizz else None
let (|Buzz|_|) i = if i % 5 = 0 then Some Buzz else None
let (|FizzBuzz|_|) i = if i % 5 = 0 && i % 3 = 0 then Some FizzBuzz else None
let findMatch = function
| Some Fizz -> "Fizz"
| Some Buzz -> "Buzz"
| Some FizzBuzz -> "FizzBuzz"
| _ -> ""
let fizzBuzz = seq {for i in 0 .. 100 -> Some i}
|> Seq.map (fun i -> i, findMatch i)
這是基本正確的方法,或者是有在這裏使用活動模式更好的方法?我應該不能讓findMatch
採取int而不是int選項?
+1爲完整的活動模式(打我吧) –