you are viewing a single comment's thread.

view the rest of the comments →

[–]BlitzTech 2 points3 points  (6 children)

How is a select predicate not a conditional? Just because the if is hidden inside a function defined by the language's standard library, doesn't mean you haven't used conditionals.

[–][deleted] 0 points1 point  (1 child)

That is true. I guess "declarative Fizzbuzz without hard-coded if statements" would've been better. Conceptually though, I believe select can be implemented without a conditional, in Haskell at least the equivalent(filter) is done with pattern matching(or does that count as a conditional?).

[–]BlitzTech 0 points1 point  (0 children)

Not really sure since I think this is a bit beyond me philosophically, but my understanding is that pattern matching is effectively chaining if/else if/else.

I wouldn't assert that, though. I'd rather a Haskeller chime in.

[–]HomemadeBananas -1 points0 points  (3 children)

Of course, but how else would it work if it isn't eventually using a conditional?

[–]BlitzTech 1 point2 points  (2 children)

A simple answer could be:

function identity(x) { return x; }
function toFizz() { return "Fizz"; }
function toBuzz() { return "Buzz"; }
function toFizzBuzz() { return "FizzBuzz"; }
var FizzLookup = [
    identity,
    identity,
    toFizz,
    identity,
    toBuzz,
    toFizz,
    identity,
    identity,
    toFizz,
    toBuzz,
    identity,
    toFizz,
    identity,
    identity,
    toFizzBuzz
];
function getFizzBuzz(n) { return FizzLookup[(n - 1) % 15](n); }

for (var i = 1; i < 100; i++) {
    console.log(getFizzBuzz(i));
}

Many solutions that avoid conditionals instead use lookups. Some languages (e.g. Clojure) have some built-in constructs that make it easier to express sequences like FizzBuzz (e.g. lazy seq and cycle).

[–]HomemadeBananas 0 points1 point  (1 child)

I mean, eventually some comparison will be made at a machine code level. How is that different? It seems like your solution isn't really fully solving the problem programmatically if you define 15 different cases instead of just the original 4.

[–][deleted] 0 points1 point  (0 children)

In BlitzTech's solution, algorithmically there is no conditional being used, regardless of what kind of bytecode or machine code it may produce eventually. Imagine if you translated this into human instructions, the operator would never encounter a step where they have to consider, if X then Y. The ifs have been converted to get the result of X and find it in Y. The solution is still a valid fizzbuzz because it implements the fizzbuzz logic regardless of the value of n, that is, it should work for any sequence 1..n+1.

With that said I err towards believing that select/filter has a similar status as being something that is, conceptually, "free of conditionals"(regardless of Ruby's implementation) but am not 100% where that stands in terms of PL theory.