How to resolve the algorithm 99 bottles of beer step by step in the Standard ML programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 99 bottles of beer step by step in the Standard ML programming language

Table of Contents

Problem Statement

Display the complete lyrics for the song:     99 Bottles of Beer on the Wall.

The lyrics follow this form: ... and so on, until reaching   0     (zero). Grammatical support for   1 bottle of beer   is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 99 bottles of beer step by step in the Standard ML programming language

Source code in the standard programming language

fun bottles 0 = ()
  | bottles x = ( print (Int.toString x ^ " bottles of beer on the wall\n");
                  print (Int.toString x ^ " bottles of beer\n");
                  print "Take one down, pass it around\n";
                  print (Int.toString (x-1) ^ " bottles of beer on the wall\n");
                  bottles (x-1)
                )

fun capitalize s =
  (str o Char.toUpper o String.sub) (s, 0) ^ String.extract (s, 1, NONE)

fun unfoldN (0, _, _) = []
  | unfoldN (n, f, init) =
      (fn (item, next) => item :: unfoldN (n - 1, f, next)) (f init)

fun textBlocks 0 = ("no more bottles", "Go to the store and buy some more", 99)
  | textBlocks i = (if i = 1 then "1 bottle" else Int.toString i ^ " bottles",
      "Take one down and pass it around", i - 1)

fun clauses i =
  let
    val (f, s, n) = textBlocks i
    val f2 = f ^ " of beer"
  in
    (f2 ^ " on the wall", f2, s, n)
  end

fun makeLine l =
  concat (ListPair.map op^ (l, [", ", ".\n"]))

fun verse (f1, f2, s, n) =
  let
    val next as (f1', f2', _, _) = clauses n
  in
    (makeLine [capitalize f1, f2] ^ makeLine [s, f1'], next)
  end

val () = (print o String.concatWith "\n" o unfoldN) (100, verse, clauses 99)

  

You may also check:How to resolve the algorithm Generator/Exponential step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Factorial base numbers indexing permutations of a collection step by step in the Haskell programming language
You may also check:How to resolve the algorithm Super-d numbers step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Retro programming language
You may also check:How to resolve the algorithm Tau function step by step in the APL programming language