Live data from Hacker News

FizzBuzz in ten languages

iolivia.me

41–50 of 103 posts

Re: FizzBuzz in ten languages

#45
An Ada version using predicates:

  with Ada.Text_IO;

  procedure FizzBuzz_Predicate is
     subtype Div_3 is Integer
        with Dynamic_Predicate => Div_3 mod 3 = 0;
     subtype Div_5 is Integer
        with Dynamic_Predicate => Div_5 mod 5 = 0;
  begin
     for i in Integer range 1 .. 99 loop
        if i in Div_3 and i in Div_5 then
           Ada.Text_IO.Put_Line ("FizzBuzz");
        elsif i in Div_3 then
           Ada.Text_IO.Put_Line ("Fizz");
        elsif i in Div_5 then
           Ada.Text_IO.Put_Line ("Buzz");
        else
           Ada.Text_IO.Put_Line (Integer'Image (i));
        end if;
     end loop;
  end FizzBuzz_Predicate;

Re: FizzBuzz in ten languages

#47
post #28

Your bash example could be a little cleaner. Since 0 is truthy in bash and [ `expr something` ] is, for most purposes, (( )). for i in {1..100}; do if (( $i % 3 && $i % 5 )); then echo FizzBuzz elif (( $i % 3 )); then echo Fizz elif (( $i % 5 )); then echo Buzz else echo $i fi done Bash also has a decently powerful matching statement so you can do something similar to the rust example. for i in {1..100}; do case "$((…

  #/bin/bash
  for n in {1..100} ; do
    f=""
    ((n % 3)) || f="Fizz"
    ((n % 5)) || f="${f}Buzz"
    echo ${f:-$n}
  done
Post reply on HN