-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples-from-book.text
1334 lines (1040 loc) · 27.7 KB
/
examples-from-book.text
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-*-Indented-Text-*-
This file contains the examples, taken directly from the Dylan manual.
This file is not in an executable format.
Page 27
? "abc"
"abc"
? 123
123
? foo:
foo:
? #\a
#\a
? #t
#t
? #f
#f
? (quote foo)
foo
? 'foo
foo
? '(1 2 3)
(1 2 3)
Page 28-29
? <window>
{the class <window>}
? concatenate
{the generic function concatenate}
? (define my-variable 25)
my-variable
? my-variable
25
? (bind ((x 50))
(+ x x))
100
? (setter element)
{the generic function (setter element)}
? (define (setter my-variable) 20)
(setter my-variable)
? (setter my-variable)
20
Page 29
? (+ 3 4)
7
? (* my-variable 3)
75
? (* (+ 3 4) 5)
35
? ((if #t + *) 4 5)
9
Page 30
; Creates and initializes a module variable
(define my-variable 25)
; Sets the value to 12
(set! my-variable 12)
; Returns 30. Uses lexical variables x and y.
(bind ((x 10) (y 20))
(+ x y))
; Creates an anonymous method, which expects 2
; numeric arguments.
(method ((a <number>) (b <number>))
(list (- a b) (+ a b)))
Page 30
? (values 1 2 3)
1
2
3
? (define-method edges ((center <number>)(radius <number>))
(values (- center radius) (+ center radius)))
edges
? (edges 100 2)
98
102
Page 32
? foo
error: unbound variable foo
? (define foo 10)
foo
? foo
10
? (+ foo 100)
110
? bar
error: unbound variable bar
? (define bar foo)
bar
? bar
10
? (define foo 20)
warning: redefining variable foo
? foo
20
? bar
10
? (+ foo bar)
30
Page 33
? (bind ((number1 20))
(number2 30))
(+ number1 number2))
50
Page 33
? (bind ((x 20)
(y (+ x x)))
(+ y y))
80
Page 33
? (define foo 10)
foo
? (+ foo foo)
20
? (bind ((foo 35))
(+ foo foo))
70
? (bind ((foo 20))
(bind ((foo 50))
(+ foo foo)))
100
Page 34
? (bind (((x <integer>) (sqrt 2)))
x)
error: 1.4142135623730951 is not an instance of <integer>
Page 34
? (bind ((foo bar baz (values 1 2 3)))
(list foo bar baz))
(1 2 3)
? (define-method opposite-edges ((center <number>)
(radius <number>))
(bind ((min max (edges center radius)))
(values max min)))
opposite-edges
? (opposite-edges 100 2)
102
98
Page 34
? (bind ((x 10)
(y 20))
(bind ((x y (values y x)))
(list x y)))
(20 10)
Page 34
? (bind ((#rest nums (edges 100 2)))
nums)
(98 102)
Page 41
? (double 10)
error: unbound variable double.
Page 41
? (define-method double ((thing <number>))
(+ thing thing))
double
? double
{the generic function double}
? (double 10)
20
Page 41
? (double "the rain in Spain.")
error: no method for {the generic function double} was found
for the arguments ("the rain in Spain.")
Page 41
? (define-method double ((thing <sequence>))
(concatenate thing thing))
double
? (double "the rain in Spain.")
"the rain in Spain.the rain in Spain."
? (double '(a b c))
(a b c a b c)
Page 43
? (define-method show-rest (a #rest b)
(print a)
(print b)
#t)
show-rest
? (show-rest 10 20 30 40)
10
(20 30 40)
#t
? (show-rest 10)
10
()
#t
Page 44
(define-method percolate (#key (brand 'maxwell-house)
(cups 4)
(strength 'strong))
(make-coffee brand cups strength))
(define-method layout (widget #key (position: the-pos)
(size: the-size))
(bind ((the-sibling (sibling widget)))
(unless (= the-pos (position the-sibling))
(align-objects widget the-sibling the-pos the-size))
Page 44
(percolate brand: 'folgers cups: 10)
(percolate strength: 'weak
brand: 'tasters-choice
cups: 1)
(layout my-widget position: (point 10 10)
size: (point 30 50))
(layout my-widget size: (query-user-for-size))
Page 45
? (define-method show-keys (req1 req2 #key foo)
(format #t "requireds: ~a ~a~%" req1 req2)
(format #t "key: ~a" foo)
#t)
show-keys
? (show-keys 'one 'two foo: 'three)
requireds: one two
key: three
#t
? (show-keys foo: 'three)
requireds: foo: three
key: #f
#t
Page 46
? (define-method label ((x <object>) #key price)
(list price x))
label
? (define-method label ((x <sequence>) #key unit-price)
(add x (* unit-price (length x))))
label
? (define-method label ((x <list>) #rest info #key calories)
(add x calories))
label
? (label 'grape price: 189 unit-price: 2)
error: illegal keyword argument unit-price:. Accepted keyword arguments are (price:).
? (label 'grape price: 189)
(189 grape)
? (label (vector 3 4 5) price: 189 unit-price: 2)
#(6 3 4 5)
? (label (vector 3 4 5) protein: 7 fat: 8 calories: 9)
error: illegal keyword argument protein:. Accepted keyword arguments are (price: unit-price:).
? (label (list 3 4 5) protein: 7 fat: 8 calories: 9)
(9 3 4 5)
Page 46
? (define-method test (the-req #rest the-rest
#key a b)
(print the-req)
(print the-rest)
(print a)
(print b))
test
? (test 1 a: 2 b: 3 c: 4)
1
(a: 2 b: 3 c: 4)
2
3
Page 49
(define-class <point> (<object>)
horizontal
vertical)
Page 49
(horizontal my-point)
Page 49
((setter horizontal) my-point 10)
Page 50
(set! (horizontal my-point) 10)
Page 51
? (define-class <menu> (<object>)
title
action)
Page 55
? (define-class <rectangle> (<object>)
(top type: <integer>
init-value: 0
init-keyword: top:)
(left type: <integer>
init-value: 0
init-keyword: left:)
(bottom type: <integer>
init-value: 100
init-keyword: bottom:)
(right type: <integer>
init-value: 100
init-keyword: right:))
<rectangle>
? <rectangle>
{the class <rectangle>}
? (define my-rectangle (make <rectangle> top: 50 left: 50))
my-rectangle
? (top my-rectangle)
50
? (bottom my-rectangle)
100
? (set! (bottom my-rectangle) 55)
55
? (bottom my-rectangle)
55
? (set! (bottom my-rectangle) 'foo)
error: foo is not an instance of <integer> while executing (setter bottom).
Page 58
(define-class <view> (<object>)
(position allocation: instance)
...)
(define-class <displaced-view> (<view>)
(position allocation: virtual)
...)
(define-method position ((v <displaced-view>))
(displace-transform (next-method v)))
(define-method (setter position) ((v <displaced-view>)
new-position)
(next-method v (undisplace-transform new-position)))
Page 59
(define-class <shape> (<view>)
(image allocation: virtual)
(cached-image allocation: instance init-value: #f)
...)
(define-method image ((shape <shape>))
(or (cached-image shape)
(set! (cached-image shape) (compute-image shape))))
(define-method (setter image) ((shape <shape>) new-image)
(set! (cached-image shape) new-image))
Page 61
? (define foo 10)
10
? foo ;this is a variable
10 ;this is the variable's contents
? (set! foo (+ 10 10))
20
? foo
20
? (setter element) ;this is a variable
{generic function (setter element)} ;the variable's contents
? (set! (setter element) %set-element)
{primitive function %set-element}
? (id? (setter element) %set-element)
#t
Page 62
? (define foo (vector 'a 'b 'c 'd))
foo
? foo
#(a b c d)
? (element foo 2)
c
? (set! (element foo 2) 'sea)
sea
? (element foo 2)
sea
? foo
#(a b sea d)
Page 64
? (define-method test ((thing <object>))
(if thing
#t
#f))
test
? (test 'hello)
#t
? (test #t)
#t
? (test #f)
#f
? (define-method double-negative ((num <number>))
(if (< num 0)
(+ num num)
num))
double-negative
? (double-negative 11)
11
? (double-negative -11)
-22
Page 65
? (define-method show-and-tell ((thing <object>))
(if thing
(begin
(print thing)
#t)
#f))
show-and-tell
? (show-and-tell "hello")
hello
#t
Page 65
(when (bonus-illuminated? pinball post)
(add-bonus-score current-player 100000))
Page 65
(unless (detect-gas? nose)
(light match))
Page 66
(cond ((< new-position old-position)
"the new position is less")
((= new-position old-position)
"the positions are equal")
(else: "the new position is greater"))
Page 67
(case (career-choice student)
((art music drama)
(print "Don't quit your day job."))
((literature history linguistics)
(print "That really is fascinating."))
((science math engineering)
(print "Say, can you fix my VCR?"))
(else: "I wish you luck."))
Page 67
(select my-object instance?
((<window> <view> <rectangle>) "it's a graphic object")
((<number> <list> <sequence>) "it's something computational")
(else: "Don't know what it is"))
Page 68
? (if #t
(print "it was true")
#t
#f)
error: too many arguments to if.
? (if #t
(begin (print "it was true")
#t)
#f)
"it was true"
#t
Page 69
(define-method factorial ((n <integer>))
(for ((i n (- i 1)) ;variable clause 1
(v 1 (* v i))) ;variable clause 2
((<= i 0) v)) ;end test and result
Page 69
(define-method first-even ((s <sequence>))
(for-each ((number s))
((even? number) number)
; No body forms needed
))
Page 70
(define-method schedule-olympic-games ((cities <sequence>)
(start-year <number>))
(for-each ((year (range from: start-year by: 4))
(city cities))
() ; No end test needed.
(schedule-game city year)))
Page 70
? (begin
(dotimes (i 6) (print "bang!"))
(print "click!"))
bang!
bang!
bang!
bang!
bang!
bang!
click!
Page 71
? (define-method first-even ((seq <sequence>))
(bind-exit (exit)
(do (method (item)
(when (even? item)
(exit item)))
seq)))
first-even
? (first-even '(1 3 5 4 7 9 10))
4
Page 72
? +
{the generic function +}
? '+
+
? (quote +)
+
? ''+
(quote +)
? (+ 10 10)
20
? '(+ 10 10)
(+ 10 10)
? (quote (+ 10 10))
(+ 10 10)
Page 73
? (apply + 1 '(2 3))
6
? (+ 1 2 3)
6
? (define math-functions (list + * / ))
math-functions
? math-functions
({method +} {method *} {method /} {method })
? (first math-functions)
{method +}
? (apply (first math-functions) 1 2 '(3 4))
10
Page 79
? (method (num1 num2)
(+ num1 num2))
{an anonymous method}
Page 80
;the second argument to SORT is the test function
? (sort person-list
(method (person1 person2)
(< (age person1)
(age person2))))
? (bind ((double (method (number)
(+ number number))))
(double (double 10)))
40
Page 80
? (define-method double ((my-method <function>))
(method (#rest args)
(apply my-method args)
(apply my-method args)
#f))
double
? (define print-twice (double print))
print-twice
? print-twice
{an anonymous method}
? (print-twice "The rain in Spain. . .")
The rain in Spain. . .The rain in Spain. . .
#f
? (print-twice 55)
5555
#f
Page 81
? (define-method root-mean-square ((s <sequence>))
(bind-methods ((average (numbers)
(/ (reduce1 + numbers)
(length numbers)))
(square (n) (* n n)))
(sqrt (average (map square s)))))
root-mean-square
? (root-mean-square '(5 6 6 7 4))
5.692099788303083
Page 81
? (define-method newtons-sqrt (x)
(bind-methods ((sqrt1 (guess)
(if (close? guess)
guess
(sqrt1 (improve guess))))
(close? (guess)
(< (abs (- (* guess guess) x)) .0001))
(improve (guess)
(/ (+ guess (/ x guess)) 2)))
(sqrt1 1)))
newtons-sqrt
? (newtons-sqrt 25)
5.000000000053723
Page 82
? (define-method double ((thing <number>))
(+ thing thing))
double
Page 82
? (double 10)
20
? (double 4.5)
9.0
Page 82
? (define-method double ((thing <integer>))
(* thing 2))
double
Page 82
? (define-method double ((thing (singleton 'cup)))
'pint)
double
? (double 'cup)
pint
Page 83
? (define-method double ((num <float>))
(print "doubling a floating-point number")
(next-method))
double
? (double 10.5)
doubling a floating-point number
21.0
Page 85
(define-method show ((device <window>) (thing <character>))
...)
(define-method show ((device <window>) (thing <string>))
...)
(define-method show ((device <window>) (thing <rectangle>))
. . .)
(define-method show ((device <file>) (thing <character>))
. . .)
(define-method show ((device <file>) (thing <string>))
. . .)
Page 86
? (make <generic-function> required: 3)
{an anonymous generic function}
? (make <generic-function> required: 3
debug-name: 'foo)
{the generic function foo}
? (define expand
(make <generic-function> required: 1 debug-name: 'expand))
{the generic function expand}
? (expand 55)
error: no applicable method for 55 in {the generic function expand}
Page 97
? (define-method double ((thing (singleton 'cup)))
'pint)
double
? (double 'cup)
pint
? (double 10)
20
Page 98
? (define-method factorial ((num <integer>))
(* num (factorial (- num 1))))
factorial
? (define-method factorial ((num (singleton 0)))
1)
factorial
? (factorial 5)
120
Page 100
? (do (method (a b) (print (+ a b)))
'(100 100 200 200)
'(1 2 3 4))
101
102
203
204
#f
Page 101
? (map +
'(100 100 200 200)
'(1 2 3 4))
(101 102 203 204)
Page 101
? (map-as <vector> +
'(100 100 200 200)
'(1 2 3 4))
#(101 102 203 204)
Page 101
? (define x '(100 100 200 200))
x
? (map-into x + '(1 2 3 4))
(101 102 203 204)
? x
(101 102 203 204)
Page 102
? (any? > '(1 2 3 4) '(5 4 3 2))
#t
? (any? even? '(1 3 5 7))
#f
Page 102
? (every? > '(1 2 3 4) '(5 4 3 2))
#f
? (every? odd? '(1 3 5 7))
#t
Page 102
? (define high-score 10)
high-score
? (reduce max high-score '(3 1 4 1 5 9))
10
? (reduce max high-score '(3 12 9 8 8 6))
12
Page 103
? (reduce1 + '(1 2 3 4 5))
15
Page 103
? (define flavors #(chocolate pistachio pumpkin))
flavors
? (member? 'chocolate flavors)
#t
? (member? 'banana flavors)
#f
Page 103
? flavors
(chocolate pistachio pumpkin)
? (find-key flavors has-nuts?)
1
? (element flavors 1)
pistachio
Page 104
? (define numbers (list 10 13 16 19))
numbers
? (replace-elements! numbers odd? double)
(10 26 16 38)
Page 104
? (define x (list 'a 'b 'c 'd 'e 'f))
x
? (fill! x 3 start: 2)
(a b 3 3 3 3)
Page 105
? (define numbers '(3 4 5))
numbers
? (add numbers 1)
(1 3 4 5)
? numbers
(3 4 5)
Page 105
? (define numbers (list 3 4 5))
numbers
? (add! numbers 1)
(1 3 4 5)
Page 105
? (add-new '(3 4 5) 1)
(1 3 4 5)
? (add-new '(3 4 5) 4)
(3 4 5)
Page 105
? (add-new! (list 3 4 5) 1)
(1 3 4 5)
? (add-new! (list 3 4 5) 4)
(3 4 5)
Page 106
? (remove '(3 1 4 1 5 9) 1)
(3 4 5 9)
Page 106
? (remove! (list 3 1 4 1 5 9) 1)
(3 4 5 9)
Page 106
? (choose even? '(3 1 4 1 5 9))
(4)
Page 106
? (choose-by even? (range from: 1)
'(a b c d e f g h i))
(b d f h)
Page 107
? (intersection '(john paul george ringo)
'(richard george edward charles john))
(john george)
Page 107
? (union '(butter flour sugar salt eggs)
'(eggs butter mushrooms onions salt))
(salt butter flour sugar eggs mushrooms onions)
Page 107
? (remove-duplicates '(spam eggs spam sausage spam spam spam))
(spam eggs sausage)
Page 108
? (remove-duplicates! '(spam eggs spam sausage spam spam))
(spam eggs sausage)
Page 108
? (define hamlet '(to be or not to be))
hamlet
? (id? hamlet (copy-sequence hamlet))
#f
? (copy-sequence hamlet start: 2 end: 4)
(or not)
Page 108
? (concatenate-as <string> '(#\n #\o #\n) '(#\f #\a #\t))
"nonfat"
? (concatenate-as <vector> '(0 1 2) '(3 4 5) '(6 7 8))
#(0 1 2 3 4 5 6 7 8)
Page 108
? (concatenate "low-" "calorie")
"low-calorie"
? (concatenate '(0 1 2) '(3 4 5) '(6 7 8))
(0 1 2 3 4 5 6 7 8)
Page 109
? (define phrase "I hate oatmeal.")
phrase
? (replace-subsequence! phrase "like" start: 2)
"I like oatmeal."
Page 109
? (define x '(bim bam boom))
x
? (reverse x)
(boom bam bim)
? x
(bim bam boom)
Page 109
? (reverse! '(bim bam boom))
(boom bam bim)
Page 110
? (define numbers '(3 1 4 1 5 9))
numbers
? (sort numbers)
(1 1 3 4 5 9)
? numbers
(3 1 4 1 5 9)
Page 110
? (sort! '(3 1 4 1 5 9))
(1 1 3 4 5 9)
Page 110
? (last '(emperor of china))
china
Page 111
? (subsequence-position "Ralph Waldo Emerson" "Waldo")
6
Page 113
? (aref #(7 8 9) 1)
8
Page 113
? (set! (aref #(7 8 9) 1) 5)
#(7 5 9) ;buggy example. Should return 5
? ((setter aref) #(7 8 9) 1 5)
#(7 5 9) ;buggy example. Should return 5
Page 113
? (dimensions (make <array> dimensions: '(4 4)))
(4 4)
Page 115
? (cons 1 2)
(1 . 2)
? (cons 1 '(2 3 4 5))
(1 2 3 4 5)
Page 115
? (list 1 2 3)