[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/TMOKOo5n    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Aug 2:
; ninety-nine bottles of beer

(define (num->words n)
  (letrec ((ones '("" "one" "two" "three" "four" "five" "six"
             "seven" "eight" "nine" "ten" "eleven" "twelve"
             "thirteen" "fourteen" "fifteen" "sixteen"
             "seventeen" "eighteen" "nineteen"))
           (tens '("" "" "twenty" "thirty" "forty" "fifty"
             "sixty" "seventy" "eighty" "ninety"))
           (groups '("" "thousand" "million" "billion" "trillion"
             "quadrillion" "quintillion" "sextillion"
             "septillion" "octillion" "nonillion" "decillion"
             "undecillion" "duodecillion" "tredecillion"
             "quattuordecillion" "quindecillion" "sexdecillion"
             "septendecillion" "octodecillion" "novemdecillion"
             "vigintillion"))
           (nnn->words (lambda (n) ; three-digit numbers
             (cond ((<= 100 n)
                     (string-append
                       (list-ref ones (quotient n 100))
                       " hundred"
                       (if (positive? (modulo n 100)) " " "")
                       (nnn->words (modulo n 100))))
                   ((<= 20 n)
                     (string-append
                       (list-ref tens (quotient n 10))
                       (if (zero? (modulo n 10)) ""
                         (string-append "-" (list-ref ones (modulo n 10))))))
                   (else (list-ref ones n))))))
    (cond ((negative? n) (string-append "negative " (num->words (- n))))
          ((<= #e1e66 n) (error 'num->words "out of range"))
          ((zero? n) "zero")
          ((< n 1000) (nnn->words n))
          (else (let loop ((n n) (groups groups))
                  (let ((prev (quotient n 1000))
                        (group (modulo n 1000)))
                    (string-append
                      (if (zero? prev) ""
                        (loop prev (cdr groups)))
                      (if (zero? group) ""
                        (string-append
                          (if (positive? prev) " " "")
                          (nnn->words group)
                          (if (string=? "" (car groups)) ""
                            (string-append " " (car groups))))))))))))

(define (beer n)
  (define (upword str)
    (string-append (string (char-upcase (string-ref str 0)))
                   (substring str 1 (string-length str))))
  (let ((n-words (upword (num->words n)))
        (n1-words (upword (num->words (- n 1)))))
    (if (< 1 n)
        (begin
          (display n-words)
          (display " bottles of beer on the wall.")
          (newline)
          (display n-words)
          (display " bottles of beer.")
          (newline)
          (display "Take one down and pass it around.")
          (newline)
          (display n1-words)
          (display " bottles of beer on the wall.")
          (newline)
          (newline)
          (beer (- n 1)))
        (begin
          (display "Only one bottle of beer on the wall.")
          (newline)
          (display "Only one bottle of beer.")
          (newline)
          (display "Take it down and pass it around.")
          (newline)
          (display "Now there's no more beer on the wall.")
          (newline)))))

(beer 99)


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
Ninety-nine bottles of beer on the wall.
Ninety-nine bottles of beer.
Take one down and pass it around.
Ninety-eight bottles of beer on the wall.

Ninety-eight bottles of beer on the wall.
Ninety-eight bottles of beer.
Take one down and pass it around.
Ninety-seven bottles of beer on the wall.

Ninety-seven bottles of beer on the wall.
Ninety-seven bottles of beer.
Take one down and pass it around.
Ninety-six bottles of beer on the wall.

Ninety-six bottles of beer on the wall.
Ninety-six bottles of beer.
Take one down and pass it around.
Ninety-five bottles of beer on the wall.

Ninety-five bottles of beer on the wall.
Ninety-five bottles of beer.
Take one down and pass it around.
Ninety-four bottles of beer on the wall.

Ninety-four bottles of beer on the wall.
Ninety-four bottles of beer.
Take one down and pass it around.
Ninety-three bottles of beer on the wall.

Ninety-three bottles of beer on the wall.
Ninety-three bottles of beer.
Take one down and pass it around.
Ninety-two bottles of beer on the wall.

Ninety-two bottles of beer on the wall.
Ninety-two bottles of beer.
Take one down and pass it around.
Ninety-one bottles of beer on the wall.

Ninety-one bottles of beer on the wall.
Ninety-one bottles of beer.
Take one down and pass it around.
Ninety bottles of beer on the wall.

Ninety bottles of beer on the wall.
Ninety bottles of beer.
Take one down and pass it around.
Eighty-nine bottles of beer on the wall.

Eighty-nine bottles of beer on the wall.
Eighty-nine bottles of beer.
Take one down and pass it around.
Eighty-eight bottles of beer on the wall.

Eighty-eight bottles of beer on the wall.
Eighty-eight bottles of beer.
Take one down and pass it around.
Eighty-seven bottles of beer on the wall.

Eighty-seven bottles of beer on the wall.
Eighty-seven bottles of beer.
Take one down and pass it around.
Eighty-six bottles of beer on the wall.

Eighty-six bottles of beer on the wall.
Eighty-six bottles of beer.
Take one down and pass it around.
Eighty-five bottles of beer on the wall.

Eighty-five bottles of beer on the wall.
Eighty-five bottles of beer.
Take one down and pass it around.
Eighty-four bottles of beer on the wall.

Eighty-four bottles of beer on the wall.
Eighty-four bottles of beer.
Take one down and pass it around.
Eighty-three bottles of beer on the wall.

Eighty-three bottles of beer on the wall.
Eighty-three bottles of beer.
Take one down and pass it around.
Eighty-two bottles of beer on the wall.

Eighty-two bottles of beer on the wall.
Eighty-two bottles of beer.
Take one down and pass it around.
Eighty-one bottles of beer on the wall.

Eighty-one bottles of beer on the wall.
Eighty-one bottles of beer.
Take one down and pass it around.
Eighty bottles of beer on the wall.

Eighty bottles of beer on the wall.
Eighty bottles of beer.
Take one down and pass it around.
Seventy-nine bottles of beer on the wall.

Seventy-nine bottles of beer on the wall.
Seventy-nine bottles of beer.
Take one down and pass it around.
Seventy-eight bottles of beer on the wall.

Seventy-eight bottles of beer on the wall.
Seventy-eight bottles of beer.
Take one down and pass it around.
Seventy-seven bottles of beer on the wall.

Seventy-seven bottles of beer on the wall.
Seventy-seven bottles of beer.
Take one down and pass it around.
Seventy-six bottles of beer on the wall.

Seventy-six bottles of beer on the wall.
Seventy-six bottles of beer.
Take one down and pass it around.
Seventy-five bottles of beer on the wall.

Seventy-five bottles of beer on the wall.
Seventy-five bottles of beer.
Take one down and pass it around.
Seventy-four bottles of beer on the wall.

Seventy-four bottles of beer on the wall.
Seventy-four bottles of beer.
Take one down and pass it around.
Seventy-three bottles of beer on the wall.

Seventy-three bottles of beer on the wall.
Seventy-three bottles of beer.
Take one down and pass it around.
Seventy-two bottles of beer on the wall.

Seventy-two bottles of beer on the wall.
Seventy-two bottles of beer.
Take one down and pass it around.
Seventy-one bottles of beer on the wall.

Seventy-one bottles of beer on the wall.
Seventy-one bottles of beer.
Take one down and pass it around.
Seventy bottles of beer on the wall.

Seventy bottles of beer on the wall.
Seventy bottles of beer.
Take one down and pass it around.
Sixty-nine bottles of beer on the wall.

Sixty-nine bottles of beer on the wall.
Sixty-nine bottles of beer.
Take one down and pass it around.
Sixty-eight bottles of beer on the wall.

Sixty-eight bottles of beer on the wall.
Sixty-eight bottles of beer.
Take one down and pass it around.
Sixty-seven bottles of beer on the wall.

Sixty-seven bottles of beer on the wall.
Sixty-seven bottles of beer.
Take one down and pass it around.
Sixty-six bottles of beer on the wall.

Sixty-six bottles of beer on the wall.
Sixty-six bottles of beer.
Take one down and pass it around.
Sixty-five bottles of beer on the wall.

Sixty-five bottles of beer on the wall.
Sixty-five bottles of beer.
Take one down and pass it around.
Sixty-four bottles of beer on the wall.

Sixty-four bottles of beer on the wall.
Sixty-four bottles of beer.
Take one down and pass it around.
Sixty-three bottles of beer on the wall.

Sixty-three bottles of beer on the wall.
Sixty-three bottles of beer.
Take one down and pass it around.
Sixty-two bottles of beer on the wall.

Sixty-two bottles of beer on the wall.
Sixty-two bottles of beer.
Take one down and pass it around.
Sixty-one bottles of beer on the wall.

Sixty-one bottles of beer on the wall.
Sixty-one bottles of beer.
Take one down and pass it around.
Sixty bottles of beer on the wall.

Sixty bottles of beer on the wall.
Sixty bottles of beer.
Take one down and pass it around.
Fifty-nine bottles of beer on the wall.

Fifty-nine bottles of beer on the wall.
Fifty-nine bottles of beer.
Take one down and pass it around.
Fifty-eight bottles of beer on the wall.

Fifty-eight bottles of beer on the wall.
Fifty-eight bottles of beer.
Take one down and pass it around.
Fifty-seven bottles of beer on the wall.

Fifty-seven bottles of beer on the wall.
Fifty-seven bottles of beer.
Take one down and pass it around.
Fifty-six bottles of beer on the wall.

Fifty-six bottles of beer on the wall.
Fifty-six bottles of beer.
Take one down and pass it around.
Fifty-five bottles of beer on the wall.

Fifty-five bottles of beer on the wall.
Fifty-five bottles of beer.
Take one down and pass it around.
Fifty-four bottles of beer on the wall.

Fifty-four bottles of beer on the wall.
Fifty-four bottles of beer.
Take one down and pass it around.
Fifty-three bottles of beer on the wall.

Fifty-three bottles of beer on the wall.
Fifty-three bottles of beer.
Take one down and pass it around.
Fifty-two bottles of beer on the wall.

Fifty-two bottles of beer on the wall.
Fifty-two bottles of beer.
Take one down and pass it around.
Fifty-one bottles of beer on the wall.

Fifty-one bottles of beer on the wall.
Fifty-one bottles of beer.
Take one down and pass it around.
Fifty bottles of beer on the wall.

Fifty bottles of beer on the wall.
Fifty bottles of beer.
Take one down and pass it around.
Forty-nine bottles of beer on the wall.

Forty-nine bottles of beer on the wall.
Forty-nine bottles of beer.
Take one down and pass it around.
Forty-eight bottles of beer on the wall.

Forty-eight bottles of beer on the wall.
Forty-eight bottles of beer.
Take one down and pass it around.
Forty-seven bottles of beer on the wall.

Forty-seven bottles of beer on the wall.
Forty-seven bottles of beer.
Take one down and pass it around.
Forty-six bottles of beer on the wall.

Forty-six bottles of beer on the wall.
Forty-six bottles of beer.
Take one down and pass it around.
Forty-five bottles of beer on the wall.

Forty-five bottles of beer on the wall.
Forty-five bottles of beer.
Take one down and pass it around.
Forty-four bottles of beer on the wall.

Forty-four bottles of beer on the wall.
Forty-four bottles of beer.
Take one down and pass it around.
Forty-three bottles of beer on the wall.

Forty-three bottles of beer on the wall.
Forty-three bottles of beer.
Take one down and pass it around.
Forty-two bottles of beer on the wall.

Forty-two bottles of beer on the wall.
Forty-two bottles of beer.
Take one down and pass it around.
Forty-one bottles of beer on the wall.

Forty-one bottles of beer on the wall.
Forty-one bottles of beer.
Take one down and pass it around.
Forty bottles of beer on the wall.

Forty bottles of beer on the wall.
Forty bottles of beer.
Take one down and pass it around.
Thirty-nine bottles of beer on the wall.

Thirty-nine bottles of beer on the wall.
Thirty-nine bottles of beer.
Take one down and pass it around.
Thirty-eight bottles of beer on the wall.

Thirty-eight bottles of beer on the wall.
Thirty-eight bottles of beer.
Take one down and pass it around.
Thirty-seven bottles of beer on the wall.

Thirty-seven bottles of beer on the wall.
Thirty-seven bottles of beer.
Take one down and pass it around.
Thirty-six bottles of beer on the wall.

Thirty-six bottles of beer on the wall.
Thirty-six bottles of beer.
Take one down and pass it around.
Thirty-five bottles of beer on the wall.

Thirty-five bottles of beer on the wall.
Thirty-five bottles of beer.
Take one down and pass it around.
Thirty-four bottles of beer on the wall.

Thirty-four bottles of beer on the wall.
Thirty-four bottles of beer.
Take one down and pass it around.
Thirty-three bottles of beer on the wall.

Thirty-three bottles of beer on the wall.
Thirty-three bottles of beer.
Take one down and pass it around.
Thirty-two bottles of beer on the wall.

Thirty-two bottles of beer on the wall.
Thirty-two bottles of beer.
Take one down and pass it around.
Thirty-one bottles of beer on the wall.

Thirty-one bottles of beer on the wall.
Thirty-one bottles of beer.
Take one down and pass it around.
Thirty bottles of beer on the wall.

Thirty bottles of beer on the wall.
Thirty bottles of beer.
Take one down and pass it around.
Twenty-nine bottles of beer on the wall.

Twenty-nine bottles of beer on the wall.
Twenty-nine bottles of beer.
Take one down and pass it around.
Twenty-eight bottles of beer on the wall.

Twenty-eight bottles of beer on the wall.
Twenty-eight bottles of beer.
Take one down and pass it around.
Twenty-seven bottles of beer on the wall.

Twenty-seven bottles of beer on the wall.
Twenty-seven bottles of beer.
Take one down and pass it around.
Twenty-six bottles of beer on the wall.

Twenty-six bottles of beer on the wall.
Twenty-six bottles of beer.
Take one down and pass it around.
Twenty-five bottles of beer on the wall.

Twenty-five bottles of beer on the wall.
Twenty-five bottles of beer.
Take one down and pass it around.
Twenty-four bottles of beer on the wall.

Twenty-four bottles of beer on the wall.
Twenty-four bottles of beer.
Take one down and pass it around.
Twenty-three bottles of beer on the wall.

Twenty-three bottles of beer on the wall.
Twenty-three bottles of beer.
Take one down and pass it around.
Twenty-two bottles of beer on the wall.

Twenty-two bottles of beer on the wall.
Twenty-two bottles of beer.
Take one down and pass it around.
Twenty-one bottles of beer on the wall.

Twenty-one bottles of beer on the wall.
Twenty-one bottles of beer.
Take one down and pass it around.
Twenty bottles of beer on the wall.

Twenty bottles of beer on the wall.
Twenty bottles of beer.
Take one down and pass it around.
Nineteen bottles of beer on the wall.

Nineteen bottles of beer on the wall.
Nineteen bottles of beer.
Take one down and pass it around.
Eighteen bottles of beer on the wall.

Eighteen bottles of beer on the wall.
Eighteen bottles of beer.
Take one down and pass it around.
Seventeen bottles of beer on the wall.

Seventeen bottles of beer on the wall.
Seventeen bottles of beer.
Take one down and pass it around.
Sixteen bottles of beer on the wall.

Sixteen bottles of beer on the wall.
Sixteen bottles of beer.
Take one down and pass it around.
Fifteen bottles of beer on the wall.

Fifteen bottles of beer on the wall.
Fifteen bottles of beer.
Take one down and pass it around.
Fourteen bottles of beer on the wall.

Fourteen bottles of beer on the wall.
Fourteen bottles of beer.
Take one down and pass it around.
Thirteen bottles of beer on the wall.

Thirteen bottles of beer on the wall.
Thirteen bottles of beer.
Take one down and pass it around.
Twelve bottles of beer on the wall.

Twelve bottles of beer on the wall.
Twelve bottles of beer.
Take one down and pass it around.
Eleven bottles of beer on the wall.

Eleven bottles of beer on the wall.
Eleven bottles of beer.
Take one down and pass it around.
Ten bottles of beer on the wall.

Ten bottles of beer on the wall.
Ten bottles of beer.
Take one down and pass it around.
Nine bottles of beer on the wall.

Nine bottles of beer on the wall.
Nine bottles of beer.
Take one down and pass it around.
Eight bottles of beer on the wall.

Eight bottles of beer on the wall.
Eight bottles of beer.
Take one down and pass it around.
Seven bottles of beer on the wall.

Seven bottles of beer on the wall.
Seven bottles of beer.
Take one down and pass it around.
Six bottles of beer on the wall.

Six bottles of beer on the wall.
Six bottles of beer.
Take one down and pass it around.
Five bottles of beer on the wall.

Five bottles of beer on the wall.
Five bottles of beer.
Take one down and pass it around.
Four bottles of beer on the wall.

Four bottles of beer on the wall.
Four bottles of beer.
Take one down and pass it around.
Three bottles of beer on the wall.

Three bottles of beer on the wall.
Three bottles of beer.
Take one down and pass it around.
Two bottles of beer on the wall.

Two bottles of beer on the wall.
Two bottles of beer.
Take one down and pass it around.
One bottles of beer on the wall.

Only one bottle of beer on the wall.
Only one bottle of beer.
Take it down and pass it around.
Now there's no more beer on the wall.


Create a new paste based on this one


Comments: