http://99-bottles-of-beer.net/language-ocaml-510.html


rec loop =

| 0 -> print "no more bottles of beer\n"

| 1 -> print "1 bottle of beer on the wall. Take it down, pass it around\n" ;

loop 0

| n -> print "[n] bottles of beer on the wall. Take one down, pass it around\n" ;

loop (n - 1)

.

loop 99;



http://99-bottles-of-beer.net/language-d-721.html


bottles =: 99 .

text =: "99 bottles" .

while bottles > 0 do

print "[text] of beer on the wall,";

print "[text] of beer.";

print "Take one down, pass it around,";

--bottles;

text << if bottles = 1 then "1 bottle" else "[bottles] bottles";;

done

print "[bottles] of beer on the wall.\n";



http://99-bottles-of-beer.net/language-haskell-1070.html


bottles = 

| 0 -> "no more bottles"

| 1 -> "1 bottle"

| n -> to_string n & " bottles"

.

verse =

| 0 -> "No more bottles of beer on the wall, no more bottles of beer.\n"

& "Go to the store and buy some more, 99 bottles of beer on the wall."

| n -> "[bottles n] of beer on the wall, [bottles n] of beer.\n[

]Take one down and pass it around, [bottles (n-1)

] of beer on the wall.\n"

.

iter (' verse ' print) (99 <-> 0);



for loop version


for n = 99 downto 1 do

print "[n] bottles of beer on the wall. Take one down, pass it around,\n"

done

print "1 bottle of beer on the wall. Take it down, pass it around,

no more bottles of beer.\n";



using goto


count =: 99 .

mark start

print "[count] bottles of beer on the wall. Take one down, pass it around,\n";

--count;

if count > 1 then goto start;;

print "1 bottle of beer on the wall. Take it down, pass it around,

no more bottles of beer.\n";



"do loop" version


count = 99 .

n =: 2 .

text =:

["1 bottle of beer on the wall. Take it down, pass it around,\n" ;

"no more bottles of beer on the wall.\n"] .

read x = "[x] bottles of beer on the wall. Take one down, pass it around,\n" .

output =

do n '

| =count -> read n :: text

|} text << read n :: text ;

++n;

done

.

map print output


This is not an appropriate use of that construct.



fizzbuzz


Let's also print "Fuzz" for multiples of 7.


f x =

if (x mod 3 = 0) then "Fizz" else "" ;;

& if (x mod 5 = 0) then "Buzz" else "" ;;

& if (x mod 7 = 0) then "Fuzz" else "" ;;

.

for i = 1 to 100 do

print "[f i]\n"

done




 




back to index