f# - FizzBuzz with Active Patterns -


i'm trying understand active patterns, i'm playing around fizzbuzz:

let (|fizz|_|) = if % 3 = 0 fizz else none let (|buzz|_|) = if % 5 = 0 buzz else none let (|fizzbuzz|_|) = if % 5 = 0 && % 3 = 0 fizzbuzz else none  let findmatch = function     | fizz -> "fizz"     | buzz -> "buzz"     | fizzbuzz -> "fizzbuzz"     | _ -> ""  let fizzbuzz = seq {for in 0 .. 100 -> i}                 |> seq.map (fun -> i, findmatch i) 

is right approach, or there better way use active patterns here? shouldn't able make findmatch take int instead of int option?

your findmatch function should be:

let findmatch = function     | fizzbuzz -> "fizzbuzz" (* should first, pad pointed out *)     | fizz -> "fizz"     | buzz -> "buzz"     | _ -> "" 

you can rewrite last few lines:

let fizzbuzz = seq.init 100 (fun -> i, findmatch i) 

your active patterns fine. 1 alternative use complete active pattern:

let (|fizz|buzz|fizzbuzz|num|) =      match % 3, % 5     | 0, 0 -> fizzbuzz     | 0, _ -> fizz     | _, 0 -> buzz     | _ -> num 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -