So I'm in need of a little assistance or at least a point in the right direction! I'm new to Haskell but I'm familiar with C# and PHP.
I'm trying to create a FizzBuzz function that allows 3 parameters to be entered. I understand the general concept of FizzBuzz but I'm trying to be able to create a function that allows you to put the first divisor, second divisor and the last parameter as an upper range. The part where I'm struggling to understand is how you assign input values to variables in a function.
I found different tutorials showing how to do the regular FizzBuzz in Haskell.
So my main questions would be this:
How do you assign input values to variables? I know how to assign the type, which would be something like this but I don't know how you would reference it in a function.
fz' :: [Integer, Integer, Integer] -> Integer -> Integer -> Integer
From what I've read online, it's better to separate the functions instead of having one large function to perform everything. With that being said, would it be best to have:
a. One function receive the input values and assign them variables, then call separate functions?
b. In the separate functions, set the range and then do
divideBy
ormod
to check if valuex
is divisible by[1..z]
print fizz, ifx
andy
are divisble by[1..z]
print fizzbuzz, ify
is divisible by[1..z]
print buzz? Is it better to use awhere
clause orcase
?c. Separate function that implements the values for the range and
(x,y,z)
?..
Any ideas, tips, help?
In PHP you define a function like this:
In Haskell you would do it this way:
and if you wanted to add a type signature:
A Haskell version of your generalized fizz-buzz might be defined like this:
Note that
myFizzBuzz
returns a list of Strings - it doesn't print them out. To print them out just join the resulting words withlines
and callputStrLn
:This also illustrates the separation of IO from computation which is prevalent in Haskell programs: you structure your program as mainly a pure computation surrounded by a thin IO-layer. In this case
myFizzBuzz
is your pure function. A complete program which takes it's parameters from the console might look like this:One reason to write pure functions is that they tend to be more reusable.
Instead of specifying an upper bound in
myFizzBuzz
what about writing it to generate an infinite series of "Fizz" and "Buzz" strings:Then the
myFizzBuzz
is simply thetake
function composed withallFizzBuzz
: