How to resolve the algorithm FizzBuzz step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm FizzBuzz step by step in the Haskell programming language

Table of Contents

Problem Statement

Write a program that prints the integers from   1   to   100   (inclusive).

But:

The   FizzBuzz   problem was presented as the lowest level of comprehension required to illustrate adequacy.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm FizzBuzz step by step in the Haskell programming language

Source Code 1:

This code uses pattern matching and a where clause to define the fizzbuzz function. For multiples of 15, it returns "FizzBuzz". For multiples of 3 or 5, it returns "Fizz" or "Buzz" respectively. Otherwise, it returns the string representation of the input number.

Source Code 2:

This code uses infix operators (>| and <|) to combine strings. The fizzbuzz function returns the string representation of the input number, concatenated with "Fizz" if it's divisible by 3, and with "Buzz" if it's divisible by 5.

Source Code 3:

This code uses the monad functions mapM_ and mapM to apply the fizzbuzz function to a range of numbers and print the results. The fizzbuzz function returns the string representation of the input number, or concatenates "Fizz" and "Buzz" if the number is divisible by both 3 and 5.

Source Code 4:

This code uses zip lists to interleave three lists of strings: one containing only the empty string, one containing "Fizz", and one containing "Buzz". It then zips the resulting list with the list of numbers from 1 to 100 and applies a function that concatenates the number with "Fizz" or "Buzz" if the number is divisible by 3 or 5 respectively.

Source Code 5:

This code uses the execWriter function to execute a sequence of state transitions and collect the output. The fizzbuzz function transitions to the False state when the input number is divisible by either 3 or 5, otherwise it transitions to the True state. The function also writes "Fizz" or "Buzz" to the writer if the number is divisible by 3 or 5 respectively.

Source Code 6:

This code uses pattern matching and the bool function to define the fizzBuzz function. For multiples of 15, it returns "FizzBuzz". For multiples of 3 or 5, it returns "Fizz" or "Buzz" respectively. Otherwise, it returns the string representation of the input number.

Source Code 7:

This code uses the max function from the Data.Monoid module to combine strings. The fizzbuzz function returns the string representation of the input number, concatenated with "fizz", "buzz", and "quxx" if the number is divisible by 3, 5, and 7 respectively.

Source Code 8:

This code uses pattern matching on the remainders of the input number when divided by 3 and 5 to define the fizzbuzz function. For numbers divisible by both 3 and 5, it returns "FizzBuzz". For numbers divisible by 3 or 5, it returns "Fizz" or "Buzz" respectively. Otherwise, it returns the string representation of the input number.

Source Code 9:

This code uses a helper function wordthing to define the fizzbuzz function. The wordthing function takes a list of pairs of numbers and strings, and an integer, and returns a string. It checks if the integer is divisible by any of the numbers in the list, and if so, it concatenates the corresponding strings. If not, it returns the string representation of the integer. The fizzbuzz function defines a list of pairs of numbers and strings for Fizz (3, "Fizz") and Buzz (5, "Buzz"), and uses wordthing to check if the input integer is divisible by either 3 or 5.

Source code in the haskell programming language

fizzbuzz :: Int -> String
fizzbuzz x
  | f 15 = "FizzBuzz"
  | f 3 = "Fizz"
  | f 5 = "Buzz"
  | otherwise = show x
  where
    f = (0 ==) . rem x

main :: IO ()
main = mapM_ (putStrLn . fizzbuzz) [1 .. 100]

fizzbuzz :: Int -> String
fizzbuzz n =
  '\n' :
  if null (fizz ++ buzz)
    then show n
    else fizz ++ buzz
  where
    fizz =
      if mod n 3 == 0
        then "Fizz"
        else ""
    buzz =
      if mod n 5 == 0
        then "Buzz"
        else ""

main :: IO ()
main = putStr $ concatMap fizzbuzz [1 .. 100]

main = mapM_ (putStrLn . fizzbuzz) [1..100]

fizzbuzz n = 
    show n <|> [fizz| n `mod` 3 == 0] ++ 
               [buzz| n `mod` 5 == 0]

-- A simple default choice operator. 
-- Defaults if both fizz and buzz fail, concats if any succeed.
infixr 0 <|>
d <|> [] = d
_ <|> x = concat x

fizz = "Fizz"
buzz = "Buzz"

main = mapM_ putStrLn $ take 100 $ zipWith show_number_or_fizzbuzz [1..] fizz_buzz_list           

show_number_or_fizzbuzz x y = if null y then show x else y

fizz_buzz_list = zipWith (++) (cycle ["","","Fizz"]) (cycle ["","","","","Buzz"])

import Control.Applicative ( ZipList(ZipList, getZipList) )

fizzBuzz :: [String]
fizzBuzz =
  getZipList $ go <$> 
    ZipList (cycle $ replicate 2 [] <> ["fizz"]) <*>
    ZipList (cycle $ replicate 4 [] <> ["buzz"]) <*>
    ZipList (show <$> [1 ..])

go :: String -> String -> String -> String
go f b n
  | null f && null b = n
  | otherwise = f <> b


main :: IO ()
main = mapM_ putStrLn $ take 100 fizzBuzz

import Data.Bool (bool)

fizzBuzz :: [String]
fizzBuzz =
  let fb n k = cycle $ replicate (pred n) [] <> [k]
   in zipWith
        (flip . bool <*> null)
        (zipWith (<>) (fb 3 "fizz") (fb 5 "buzz"))
        (show <$> [1 ..])

main :: IO ()
main = mapM_ putStrLn $ take 100 fizzBuzz

import Control.Monad.State
import Control.Monad.Trans
import Control.Monad.Writer

main = putStr $ execWriter $ mapM_ (flip execStateT True . fizzbuzz) [1..100]

fizzbuzz :: Int -> StateT Bool (Writer String) ()
fizzbuzz x = do
 when (x `mod` 3 == 0) $ tell "Fizz" >> put False
 when (x `mod` 5 == 0) $ tell "Buzz" >> put False
 get >>= (flip when $ tell $ show x)
 tell "\n"

fizzBuzz :: (Integral a) => a -> String
fizzBuzz i
  | fizz && buzz = "FizzBuzz"
  | fizz         = "Fizz"
  | buzz         = "Buzz"
  | otherwise    = show i
  where fizz = i `mod` 3 == 0
        buzz = i `mod` 5 == 0

main = mapM_ (putStrLn . fizzBuzz) [1..100]

import Data.Monoid

fizzbuzz = max
       <$> show
       <*> "fizz" `when` divisibleBy 3
       <>  "buzz" `when` divisibleBy 5
       <>  "quxx" `when` divisibleBy 7
  where
    when m p x = if p x then m else mempty
    divisibleBy n x = x `mod` n == 0

main = mapM_ (putStrLn . fizzbuzz) [1..100]

fizzbuzz n = case (rem n 3, rem n 5) of
               (0, 0) -> "FizzBuzz"
               (0, _) -> "Fizz"
               (_, 0) -> "Buzz"
               (_, _) -> show n

main = mapM_ (putStrLn . fizzbuzz) [1..100]

wordthing :: [(Int, String)] -> Int -> String
wordthing lst n =
  if matches == [] then
    show n
  else
    concat $ map snd matches
  where matches = filter (\x -> n `mod` (fst x) == 0) lst

fizzbuzz :: Int -> String
fizzbuzz = wordthing [(3, "Fizz"), (5, "Buzz")]

main = do
  mapM_ (putStrLn . fizzbuzz) [1..100]

  

You may also check:How to resolve the algorithm Loops/While step by step in the Sparkling programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the Haskell programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Joystick position step by step in the Haskell programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the HLA programming language