Copyright © Cay S. Horstmann 2011 
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United
States License.

Cay S. Horstmann | Nguyễn Hứa Phùng


Address Contents Mnemonic 0x80483E4: 55 push %ebp 0x80483E5: 89 45 movl %esp, %ebp 0x80483E7: 83 EC 10 movl $0x10, %esp
55 89 45 83 EC 10 ...
push, movl) and
registers (%esp)$10call fac, jmp .L1).L3: movl r, %eax imull n, %eax movl %eax, r subl $1, n .L2: cmpl $0, n jg .L3

do {
r = r * n;
n--;
} while (n > 0)



Programmer specifies how the problem should be solved
Programmer specifies what should be solved

C 99 BOTTLES OF BEER ON THE WALL
INTEGER BOTTLS
DO 50 I = 1, 99
BOTTLS = 100 - I
PRINT 10, BOTTLS
10 FORMAT(1X, I2, 31H BOTTLE(S) OF BEER ON THE WALL.)
PRINT 20, BOTTLS
20 FORMAT(1X, I2, 19H BOTTLE(S) OF BEER.)
PRINT 30
30 FORMAT(34H TAKE ONE DOWN AND PASS IT AROUND,)
BOTTLS = BOTTLS - 1
PRINT 10, BOTTLS
PRINT 40
40 FORMAT(1X)
50 CONTINUE
STOP
END
BOTTLS)C),
numeric labelDO 50 I = 1, 99 references label 50
CONTINUEPRINT 30 references format 30 FORMAT(...)
(define (down n k)
(if (< n k) '()
(cons n (down (- n 1) k))))
(define (plural? n . up)
(let ((.. string-append)
(num (number->string n))
(bot " bottle")
(ltr (if (null? up) "n" "N")))
(case n ((0) (.. ltr "o more" bot "s"))
((1) (.. num bot))
(else (.. num bot "s"))
(define (verse n)
(let ((.. string-append)
(top (plural? n 1))
(mid (plural? n))
(nxt (plural? (if (= n 0)
99
(- n 1))))
(beer " of beer")
(wall " on the wall")
(actn (if (= n 0)
"Go to the store and buy some more, "
"Take one down and pass it around, ")))
`(,(.. top beer wall ", " mid beer ".")
. ,(.. actn nxt beer wall "."))))
(define (sing verse)
(let ((n newline))
(display (car verse)) (n)
(display (cdr verse)) (n) (n)))
(for-each sing (map verse (iota 99 0)))
(fun arg1 arg2 ...)cons and taken apart with
car, cdrsing and
verse in the last line)(let ((var1 init1) (var2 init2) ...) ...)plural?,
.. 
'begin'
'comment'
99 Bottles of Beer on the Wall
;
'integer' 'procedure' bottles(n);
'value' n;
'integer' n;
'begin'
'if' n < 1 'then' outstring(1, "no more ") 'else' outinteger(1, n);
'if' n = 1 'then' outstring(1, "bottle") 'else' outstring(1, "bottles");
outstring(1, " of beer");
'end';
'integer' i;
'for' i := 99 'step' -1 'until' 1 'do' 'begin'
bottles(i); outstring(1, " on the wall, ");
bottles(i); outstring(1, "\n");
outstring(1, "take one down and pass it around, ");
bottles(i - 1); outstring(1, " on the wall.\n");
'end';
'end'
:= (= means equality, as it did for
400 years in math)'begin' ... 'end'
#define MAXBEER (99)
void chug(register int beers) {
char howmany[8], *s;
s = beers != 1 ? "s" : "";
printf("%d bottle%s of beer on the wall,\n", beers, s);
printf("%d bottle%s of beeeeer . . . ,\n", beers, s);
printf("Take one down, pass it around,\n");
if(--beers) sprintf(howmany, "%d", beers); else strcpy(howmany, "No more");
s = beers != 1 ? "s" : "";
printf("%s bottle%s of beer on the wall.\n", howmany, s);
}
main() {
int beers;
for(beers = MAXBEER; beers; chug(beers--))
puts("");
puts("\nTime to buy more beer!\n");
}
{ ... }if (--beers)for (expr1; expr2; expr3) need not be
relatedprintf, strcpy
#include <iostream>
template<int I>
class Loop {
public:
static inline void f() {
cout << I << " bottles of beer on the wall," << endl
<< I << " bottles of beer." << endl
<< "Take one down, pass it around," << endl
<< I-1 << " bottles of beer on the wall." << endl;
Loop<I-1>::f();
}
};
class Loop<0> {
public:
static inline void f() {
cout << "Go to the store and buy some more," << endl
<< "99 bottles of beer on the wall." << endl;
}
};
main() {
Loop<3>::f();
}
cout << ...)
class Verse {
private final int count;
Verse(int verse) {
count = 100-verse;
}
public String toString() {
String c =
"{0,choice,0#no more bottles|1#1 bottle|1<{0} bottles} of beer";
return java.text.MessageFormat.format(
c.replace("n","N")+" on the wall, "+c+".\n"+
"{0,choice,0#Go to the store and buy some more"+
"|0<Take one down and pass it around}, "+c.replace("{0","{1")+
" on the wall.\n", count, (count+99)%100);
}
public static void main(String[] args) {
for (int i = 1; i <= 100; i++)
System.out.println(new Verse(i));
}
}
new Verse(i)java.text.MessageFormat


