Re:Streem – a new programming language from Matz
by icedog on Thursday December 11, 2014 @02:48PM
(#8736692)
I disagree that it maps most directly to the problem statement. You're performing a common factor computation in your mind, which may be more difficult given numbers other than 3 and 5. In my opinion, pattern matching offers the most direct solution and comes with an abundance of compiler optimizations. Here's an example in Rust...
[ Parent
| on HN ]
Re:Streem – a new programming language from Matz
by dragonwriter on Thursday December 11, 2014 @03:07PM
(#8736789)
> I disagree that it maps most directly to the problem statement. You're performing a common factor computation in your mind, which may be more difficult given numbers other than 3 and 5.
Well, sure, explicitly calling out i % 15 rather than (i % 3) && (i % 5) or the equivalent has that problem.
> In my opinion, pattern matching offers the most direct solution and comes with an abundance of compiler optimizations.
Pattern matching is not available in many languages, but, sure, where its available, its a great choice. Note that this still has a distinct case for the case where both % 3 and % 5 are true, rather than just testing those cases independently and sequentially and concatenating, so I think it falls into the general class of solutions I was describing.
Re:Streem – a new programming language from Matz
by icedog on Thursday December 11, 2014 @03:16PM
(#8736835)
I'll take an extra branch over concatenation and dealing with new line chars any day.
Re:Streem – a new programming language from Matz
by qubitcoder on Thursday December 11, 2014 @05:07PM
(#8737315)
The solution in Haskell is quite clean, I believe.
fizzBuzz n
| n `mod` 15 == 0 = "FizzBuzz"
| n `mod` 3 == 0 = "Fizz"
| n `mod` 5 == 0 = "Buzz"
| otherwise = show n
main = mapM_ (print . fizzBuzz) [1..100]
I agree with you about generalizing pattern matching for less simple cases. Your example brought to mind view patterns, about which Oliver O'Charles had a nice writeup recently [1]. Nifty little extension.[1] https://ocharles.org.uk/blog/posts/2014-12-02-view-patterns....
Re:Streem – a new programming language from Matz
by ghuntley on Thursday December 11, 2014 @10:40PM
(#8738472)
Using F# pattern matching:
let buzzer number =
match number with
| i when i % 3 = 0 && i % 5 = 0 -> "FizzBuzz"
| i when i % 3 = 0 -> "Fizz"
| i when i % 5 = 0 -> "Buzz"
| i -> (sprintf "%i" i)
for i = 1 to 100 do
printfn "%s" (buzzer i)