[ create a new paste ] login | about

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

programmingpraxis - Scheme, pasted on Sep 21:
; oban numbers

(define-syntax fold-of
  (syntax-rules (range in is)
    ((_ "z" f b e) (set! b (f b e)))
    ((_ "z" f b e (v range fst pst stp) c ...)
      (let* ((x fst) (p pst) (s stp)
             (le? (if (positive? s) <= >=)))
        (do ((v x (+ v s))) ((le? p v) b)
          (fold-of "z" f b e c ...))))
    ((_ "z" f b e (v range fst pst) c ...)
      (let* ((x fst) (p pst) (s (if (< x p) 1 -1)))
        (fold-of "z" f b e (v range x p s) c ...)))
    ((_ "z" f b e (v range pst) c ...)
      (fold-of "z" f b e (v range 0 pst) c ...))
    ((_ "z" f b e (x in xs) c ...)
      (do ((t xs (cdr t))) ((null? t) b)
        (let ((x (car t)))
          (fold-of "z" f b e c ...))))
    ((_ "z" f b e (x is y) c ...)
      (let ((x y)) (fold-of "z" f b e c ...)))
    ((_ "z" f b e p? c ...)
      (if p? (fold-of "z" f b e c ...)))
    ((_ f i e c ...)
      (let ((b i)) (fold-of "z" f b e c ...)))))

(define-syntax list-of (syntax-rules ()
  ((_ arg ...) (reverse (fold-of
    (lambda (d a) (cons a d)) '() arg ...)))))

(define (string-index c str)
  (let loop ((ss (string->list str)) (k 0))
    (cond ((null? ss) #f)
          ((char=? (car ss) c) k)
          (else (loop (cdr ss) (+ k 1))))))

(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 "
                       (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))))))))))))

(for-each
  (lambda (n) (display n) (newline))
  (list-of n
    (n range 1000)
    (not (string-index #\o (num->words n)))))


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
3
5
6
7
8
9
10
11
12
13
15
16
17
18
19
20
23
25
26
27
28
29
30
33
35
36
37
38
39
50
53
55
56
57
58
59
60
63
65
66
67
68
69
70
73
75
76
77
78
79
80
83
85
86
87
88
89
90
93
95
96
97
98
99
300
303
305
306
307
308
309
310
311
312
313
315
316
317
318
319
320
323
325
326
327
328
329
330
333
335
336
337
338
339
350
353
355
356
357
358
359
360
363
365
366
367
368
369
370
373
375
376
377
378
379
380
383
385
386
387
388
389
390
393
395
396
397
398
399
500
503
505
506
507
508
509
510
511
512
513
515
516
517
518
519
520
523
525
526
527
528
529
530
533
535
536
537
538
539
550
553
555
556
557
558
559
560
563
565
566
567
568
569
570
573
575
576
577
578
579
580
583
585
586
587
588
589
590
593
595
596
597
598
599
600
603
605
606
607
608
609
610
611
612
613
615
616
617
618
619
620
623
625
626
627
628
629
630
633
635
636
637
638
639
650
653
655
656
657
658
659
660
663
665
666
667
668
669
670
673
675
676
677
678
679
680
683
685
686
687
688
689
690
693
695
696
697
698
699
700
703
705
706
707
708
709
710
711
712
713
715
716
717
718
719
720
723
725
726
727
728
729
730
733
735
736
737
738
739
750
753
755
756
757
758
759
760
763
765
766
767
768
769
770
773
775
776
777
778
779
780
783
785
786
787
788
789
790
793
795
796
797
798
799
800
803
805
806
807
808
809
810
811
812
813
815
816
817
818
819
820
823
825
826
827
828
829
830
833
835
836
837
838
839
850
853
855
856
857
858
859
860
863
865
866
867
868
869
870
873
875
876
877
878
879
880
883
885
886
887
888
889
890
893
895
896
897
898
899
900
903
905
906
907
908
909
910
911
912
913
915
916
917
918
919
920
923
925
926
927
928
929
930
933
935
936
937
938
939
950
953
955
956
957
958
959
960
963
965
966
967
968
969
970
973
975
976
977
978
979
980
983
985
986
987
988
989
990
993
995
996
997
998
999


Create a new paste based on this one


Comments: