Without giving it to much thought: here is what I might do:

1
2
3
4
5
6
7
8
9
10
 

    let fizzbuzz n = 
        let divBy d n = n % d = 0
        match (divBy 3 n), (divBy 5 n) with
        | true, true -> "FizzBuzz"
        | true, false -> "Fizz"
        | false, true -> "Buzz"
        | _ -> sprintf "%d" n

Of course you might want to change my strings with your algebraic type.

You have to divide by both numbers 3 and 5 so you can't take out this - but you can do this once and be done with it.

By on 7/22/2011 1:20 AM ()

Nice idea :)

I also saw a solution using active patterns. It also looked very clean. What I like about your solution is that you don't have then 'when' part. This is something I don't like in my solution. It looks like a kind of missuse of pattern matching.

By on 7/24/2011 11:40 AM ()

was thinking about the active patterns too.

This might gain you readability but you have to add a lot of boiler-code (the patterns-definition) and I think the patterns would fire at every match-case (so you fire the 3/5-test twice instead of just once)

By on 7/25/2011 11:57 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper