2

This is a continuation to my previous post.

I'm creating a FizzBuzz function that allows 3 parameters to be entered. However, this isn't just the standard FizzBuzz program. This one allows for 2 divisors while implementing an upper range. So if div1 is divisible print "Fizz", if div2 is divisible print "buzz", and if div1 and div2 are divisible print "FizzBuzz", else print the number/integer.

I found different tutorials showing how to do the regular FizzBuzz in Haskell and modified it to be able to do as explained above.

Right now I'm getting an error and I'm not sure how to properly do this in Haskell.

fizzbuzz' :: [(Integer, String)] -> Integer -> String
fizzbuzz' ss n = foldl (\str (num, subst) -> if n `mod` num == 0 then str ++ subst else str ++ "") "" ss

fizzbuzz :: [(Integer, String)] -> Integer -> String
fizzbuzz ss n = if null str then show n else str
  where str = fizzbuzz' ss n

fb :: Integer -> Integer -> Integer -> Integer
fb x y z = mapM_ putStrLn $ map (fizzbuzz [(x, "fizz"), (y, "buzz")]) [0..z]

The error I'm getting is:

Couldn't match expected type ‘Integer’ with actual type ‘IO ()’
In the expression:
  mapM_ putStrLn $ map (fizzbuzz [(x, "fizz"), (y, "buzz")]) [0 .. z]
In an equation for ‘fb’:
    fb x y z
      = mapM_ putStrLn
        $ map (fizzbuzz [(x, "fizz"), (y, "buzz")]) [0 .. z]
Failed, modules loaded: none.

Any ideas?

2
  • What type do you suppose fb should return? Why would you think that returns an Integer? Commented Aug 13, 2015 at 18:29
  • Please do not add the php tag unneccessarily. The fact that you know php is completely unrelated to the question. Moreover it even breaks the highlighting of code (which uses tags to select the default highlighting). Commented Aug 14, 2015 at 7:13

1 Answer 1

1

The problem is simply with your type signature for fb. Just remove it and your code will compile.

The correct type signature for fb is Integer -> Integer -> Integer -> IO () as you can verify by having ghci tell you with the :t command:

$ ghci Fizzbuzz.hs
Prelude> :t fb
fb :: Integer -> Integer -> Integer -> IO ()
Prelude>
Sign up to request clarification or add additional context in comments.

1 Comment

So simple.. can't believe I missed that! Thanks again for all your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.