r/deftruefalse • • May 26 '16

Implement FizzBuzzBazz

The FizzBuzzBazz challenge

Create a program that prints the first N entries of the FizzBuzzBazz sequence to stdout, where any (hardcoded) N between 0 and at least 2'147'483'647 (the biggest number representable by a signed 32bit integer).

The FizzBuzzBazz sequence is a simple extension of the FizzBuzz sequence. An easy implementation to get one of its elements (which obviously isn't allowed here, see rules below) would be:

function fizz_buzz_bazz(i) {
    var str = "";

    if (i % 3 == 0)
        str += "Fizz";
    if (i % 5 == 0)
        str += "Buzz";
    if (i % 7 == 0)
        str += "Bazz";

    return str || i.toString();
}

Rules

  • No mutation allowed (hence the above implementation is not allowed)
  • You're only allowed to call a single function with side effects that does IO
    • Import statements don't count in case they are ordinary functions in the language of your choice
  • You're allowed to call one extra function with side effects that does IO if you use it to read N at runtime instead of hardcoding it
  • You can use the standard library of the language you use, as well as well-known third-party libraries, but no obscure tiny libraries that are made to solve exactly this problem
  • Reminder: this sub has the rule to not submit any idiomatic code

Bonus challenges

  • Implement the logic of this program as a C++ template with N being the template parameter
  • Make all of your own functions return abnormally (e.g. throw an exception)
  • Call one less function with side effects that does IO than allowed
12 Upvotes

29 comments sorted by

View all comments

Show parent comment

2

u/ultrasu May 26 '16

Meanwhile it took me a few minutes to remember why it works, as it's based on a one-liner I wrote about year ago and mainly use Haskell, Lisp, and C these days.

1

u/jP_wanN May 26 '16

Wow, plain old C? From a person who also knows purely functional programming? Have you looked into Rust?

Apart from that, I'd actually love to see an ugly implementation of FizzBuzzBazz in Haskell or something similar. I feel like while I could decently solve some medium-sized problems with it, I still need to learn a lot to write intentionally bad Haskell :D

1

u/ultrasu May 26 '16

To be fair, I mostly use C for FFIs with other languages. Rust does look great, been meaning to learn it for months now, will hopefully have the time this summer.

I did initially try to come up with a shitty implementation in Haskell or Lisp, but not sure how without violating the no-mutation rule. Easiest way to misuse a functional languages is to abuse mutability. I'm pretty sure you can write some really weird FizzBuzzBazz implementation using Haskell's type system and/or Template Haskell and/or Control.Arrow, though that's also a bit too advanced for me at the moment.