t3x.org / sketchy / prog / bottles.html
SketchyLISP Stuff Copyright (C) 2006 Nils M Holm

bottles

Conformance: R5RS

Purpose: Print the lyrics of "99 bottles of beer on the wall".

Implementation:

(define (bottles . n)
  (letrec
  
    ((bottles (lambda (n suffix)
      (begin
        (display n)
        (display (if (= n 1) " bottle" " bottles"))
        (display suffix)
        (newline))))
      
    (verse (lambda (n)
      (begin
        (bottles n " of beer on the wall,")
        (bottles n " of beer.")
        (display "Take one down and pass it around,")
        (newline)
        (bottles (- n 1) " of beer on the wall."))))

    (count-bottles (lambda (n)
      (cond ((zero? n) 'done)
        (#t (begin (verse n)
                   (newline)
                   (count-bottles (- n 1))))))))

    (cond ((null? n)
        (count-bottles 99))
      (#t (count-bottles (car n))))))

Example:

(bottles 99)