forked from pablomarx/Thomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdylan-examples.dyl
1860 lines (1273 loc) · 30.8 KB
/
dylan-examples.dyl
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
;;;; -*- Scheme -*- isn't Thomas (or Dylan(TM))
;* Copyright 1992 Digital Equipment Corporation
;* All Rights Reserved
;*
;* Permission to use, copy, and modify this software and its documentation is
;* hereby granted only under the following terms and conditions. Both the
;* above copyright notice and this permission notice must appear in all copies
;* of the software, derivative works or modified versions, and any portions
;* thereof, and both notices must appear in supporting documentation.
;*
;* Users of this software agree to the terms and conditions set forth herein,
;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
;* right and license under any changes, enhancements or extensions made to the
;* core functions of the software, including but not limited to those affording
;* compatibility with other hardware or software environments, but excluding
;* applications which incorporate this software. Users further agree to use
;* their best efforts to return to Digital any such changes, enhancements or
;* extensions that they make and inform Digital of noteworthy uses of this
;* software. Correspondence should be provided to Digital at:
;*
;* Director, Cambridge Research Lab
;* Digital Equipment Corp
;* One Kendall Square, Bldg 700
;* Cambridge MA 02139
;*
;* This software may be distributed (but not offered for sale or transferred
;* for compensation) to third parties, provided such third parties agree to
;* abide by the terms and conditions of this notice.
;*
;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
;* SOFTWARE.
; $Id: dylan-examples.dyl,v 1.16 1992/09/23 04:43:53 birkholz Exp $
;;; This is a copy of examples-from-book.text modified to be runnable in
;;; the Thomas REP. The expected return value is given after ";Value: ".
;;; The book doesn't always show return values, so this value is what you
;;; can expect from Thomas. Printed output appears after ";" before
;;; ";Value: ". Error messages are quoted from the book. Thomas doesn't
;;; report errors in exactly the same way. Definitions added to make the
;;; example work are flagged by ";;; +". Notes about differences from the
;;; examples as given in the book are prefixed by ";;; ".
;; Page 27
"abc"
;Value: "abc"
123
;Value: 123
foo:
;Value: foo:
#\a
;Value: #\a
#t
;Value: #t
#f
;Value: #f
(quote foo)
;Value: foo
'foo
;Value: foo
'(1 2 3)
;Value: (1 2 3)
;; Page 28
;;; +
(define-class <window> (<object>))
;Value: <window>
<window>
;Value: #[dylan-class ...]
concatenate
;Value: #[generic function ...]
(define my-variable 25)
;Value: my-variable
my-variable
;Value: 25
(bind ((x 50))
(+ x x))
;Value: 100
(setter element)
;Value: #[generic function ...]
(define (setter my-variable) 20)
;Value: (setter my-variable)
(setter my-variable)
;Value: 20
;; Page 29
(+ 3 4)
;Value: 7
(* my-variable 3)
;Value: 75
(* (+ 3 4) 5)
;Value: 35
((if #t + *) 4 5)
;Value: 9
;; Page 30
; Creates and initializes a module variable
(define my-variable 25)
;Value: my-variable
; Sets the value to 12
(set! my-variable 12)
;Value: 12
; Returns 30. Uses lexical variables x and y.
(bind ((x 10) (y 20))
(+ x y))
;Value: 30
; Creates an anonymous method, which expects 2 numeric arguments.
(method ((a <number>) (b <number>))
(list (- a b) (+ a b)))
;Value: #[method ..]
(values 1 2 3)
;Value[1]: 1
;Value[2]: 2
;Value[3]: 3
(define-method edges ((center <number>) (radius <number>))
(values (- center radius) (+ center radius)))
;Value: edges
(edges 100 2)
;Value[1]: 98
;Value[2]: 102
;; Page 32
foo
;error: unbound variable foo
(define foo 10)
;Value: foo
foo
;Value: 10
(+ foo 100)
;Value: 110
bar
;error: unbound variable bar
(define bar foo)
;Value: bar
bar
;Value: 10
(define foo 20)
;;; Thomas doesn't warn that module variable foo is being redefined.
;Value: foo
foo
;Value: 20
bar
;Value: 10
(+ foo bar)
;Value: 30
;; Page 33
(bind ((number1 20)
(number2 30))
(+ number1 number2))
;Value: 50
(bind ((x 20)
(y (+ x x)))
(+ y y))
;Value: 80
(define foo 10)
;Value: foo
(+ foo foo)
;Value: 20
(bind ((foo 35))
(+ foo foo))
;Value: 70
(bind ((foo 20))
(bind ((foo 50))
(+ foo foo)))
;Value: 100
;; Page 34
(bind (((x <integer>) (sqrt 2)))
x)
;error: 1.4142135623730951 is not an instance of <integer>
(bind ((foo bar baz (values 1 2 3)))
(list foo bar baz))
;Value: (1 2 3)
(define-method opposite-edges ((center <number>)
(radius <number>))
(bind ((min max (edges center radius)))
(values max min)))
;Value: opposite-edges
(opposite-edges 100 2)
;Value[1]: 102
;Value[2]: 98
(bind ((x 10)
(y 20))
(bind ((x y (values y x)))
(list x y)))
;Value: (20 10)
(bind ((!rest nums (edges 100 2)))
nums)
;Value: (98 102)
;; Page 41
(double 10)
;error: unbound variable double
(define-method double ((thing <number>))
(+ thing thing))
;Value: double
(double 10)
;Value: 20
(double "the rain in Spain.")
;error: no method for {the generic function double} was found for the
; arguments ("the rain in Spain.")
(define-method double ((thing <sequence>))
(concatenate thing thing))
;Value: double
(double "the rain in Spain.")
;Value: "the rain in Spain.the rain in Spain."
(double '(a b c))
;Value: (a b c a b c)
;; Page 43
(define-method show-rest (a !rest b)
(print a)
(print b)
#t)
;Value: show-rest
(show-rest 10 20 30 40)
;10
;(20 30 40)
;Value: #t
(show-rest 10)
;10
;()
;Value: #t
;; Page 44
;;; +
(define-method make-coffee (!rest x) x)
;Value: make-coffee
(define-method percolate (!key (brand 'maxwell-house)
(cups 4)
(strength 'strong))
(make-coffee brand cups strength))
;Value: percolate
;;; +
(define-method position ((x <list>)) (car x))
;Value: position
;;; +
(add-method position (method ((x <number>)) x))
;Value[1]: #[method ...]
;Value[2]: ()
;;; +
(define-method sibling ((x <number>)) (+ x 5))
;Value: sibling
;;; +
(define-method align-objects (a b c d) (list a b c d))
;Value: align-objects
(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))))
;Value: layout
;; Page 44
(percolate brand: 'folgers cups: 10)
;Value: (folgers 10 strong)
(percolate strength: 'weak
brand: 'tasters-choice
cups: 1)
;Value: (tasters-choice 1 weak)
;;; +
(define my-widget 3)
;Value: my-widget
;;; +
(define-method point ((x <number>) (y <number>)) (list x y))
;Value: point
;;; +
(define-method query-user-for-size () 3)
;Value: query-user-for-size
(layout my-widget position: (point 10 10)
size: (point 30 50))
;Value: (3 8 (10 10) (30 50))
(layout my-widget size: (query-user-for-size))
;Value: (3 8 #f 3)
;; Page 45
;;; In lieu of format, print lists.
(define-method show-keys (req1 req2 !key foo)
(print (list "requireds: " req1 req2))
(print (list "key: " foo))
#t)
;Value: show-keys
(show-keys 'one 'two foo: 'three)
;("requireds: " one two)
;("key: " three)
;Value: #t
(show-keys foo: 'three)
;("requireds: " foo: three)
;("key: " #f)
;Value: #t
;; Page 46
(define-method label ((x <object>) !key price)
(list price x))
;Value: label
;;; length changed to size
(define-method label ((x <sequence>) !key unit-price)
(add x (* unit-price (size x))))
;Value: label
(define-method label ((x <list>) !rest info !key calories)
(add x calories))
;Value: label
(label 'grape price: 189 unit-price: 2)
;error: illegal keyword argument unit-price:. Accepted keyword arguments
; are (price:).
(label 'grape price: 189)
;Value: (189 grape)
(label (vector 3 4 5) price: 189 unit-price: 2)
;Value: #(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)
;Value: (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))
;Value: test
(test 1 a: 2 b: 3 c: 4)
;1
;(a: 2 b: 3 c: 4)
;2
;3
;No value
;; Page 49
(define-class <point> (<object>)
horizontal
vertical)
;Value: <point>
;; Page 49
;;; +
(define my-point (make <point>))
;Value: my-point
(horizontal my-point)
;;; The example wasn't intended to demonstrate an uninitialized slot, but
;;; it's a good thing to test here.
;error: uninitialized slot
;; Page 49
;;; +
(define my-point (make <point>))
;Value: my-point
((setter horizontal) my-point 10)
;Value: 10
;;; +
(horizontal my-point)
;Value: 10
;; Page 50
;;; +
(define my-point (make <point>))
;Value: my-point
(set! (horizontal my-point) 10)
;Value: 10
;;; +
(horizontal my-point)
;Value: 10
;; Page 51
;;; Not materially different from the definition of <point> above.
;; 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:))
;Value: <rectangle>
<rectangle>
;Value: #[dylan-class ...]
(define my-rectangle (make <rectangle> top: 50 left: 50))
;Value: my-rectangle
(top my-rectangle)
;Value: 50
(bottom my-rectangle)
;Value: 100
(set! (bottom my-rectangle) 55)
;Value: 55
(bottom my-rectangle)
;Value: 55
(set! (bottom my-rectangle) 'foo)
;error: foo is not an instance of <integer> while executing (setter bottom).
;; Page 58
;;; Punt dots.
(define-class <view> (<object>)
(position allocation: instance))
;Value: <view>
;;; Punt dots.
(define-class <displaced-view> (<view>)
(position allocation: virtual))
;Value: <displaced-view>
(define-method position ((v <displaced-view>))
(displace-transform (next-method v)))
;Value: position
(define-method (setter position) ((v <displaced-view>)
new-position)
(next-method v (undisplace-transform new-position)))
;Value: (setter position)
;;; +
(define-method displace-transform (position)
(list 'displace-transform position))
;Value: displace-transform
;;; +
(define-method undisplace-transform (position)
(list 'undisplace-transform position))
;Value: undisplace-transform
;;; +
(define my-displaced-view (make <displaced-view> position: 'initial-position))
;Value: my-displaced-view
;;; +
(position my-displaced-view)
;Value: (displace-transform initial-position)
;;; Actually getting (displace-transform ()) instead because of limitation
;;; (9) in DIFFERENCES.
;;; +
(set! (position my-displaced-view) 'next-position)
;Value: next-position
;;; +
(position my-displaced-view)
;Value: (displace-transform (undisplace-transform next-position))
;; Page 59
;;; Punt dots.
(define-class <shape> (<view>)
(image allocation: virtual)
(cached-image allocation: instance init-value: #f))
;Value: <shape>
(define-method image ((shape <shape>))
(or (cached-image shape)
(set! (cached-image shape) (compute-image shape))))
;Value: image
(define-method (setter image) ((shape <shape>) new-image)
(set! (cached-image shape) new-image))
;Value: (setter image)
;;; +
(define-method compute-image (shape)
(list 'compute-image shape))
;Value: compute-image
;;; +
(define my-shape (make <shape>))
;Value: my-shape
;;; +
(image my-shape)
;Value: (compute-image #[dylan-instance ...])
;;; +
((setter image) my-shape 'new-image)
;Value: new-image
;;; +
(image my-shape)
;Value: new-image
;; Page 61
(define foo 10)
;;; The book shows 10 being returned, but the definition of define says the
;;; variable name is returned.
;Value: foo
foo ; this is a variable
;Value: 10 ; this is the variable's contents
(set! foo (+ 10 10))
;Value: 20
foo
;Value: 20
(setter element) ; this is a variable
;Value: #[generic function ...] ; the variable's contents
;;; +
;;; Save the original value to restore for later tests.
(define %original-set-element (setter element))
;Value: %original-set-element
;;; +
(define-method %set-element (seq index value)
(print (list '%set-element seq index value))
value)
;Value: %set-element
(set! (setter element) %set-element)
;Value: #[generic function ...]
(id? (setter element) %set-element)
;Value: #t
;;; The next two should also work.
;;; +
(set! (element 'foo 'bar) 'baz)
;(%set-element foo bar baz)
;Value: baz
;;; +
((setter element) 'foo 'bar 'baz)
;(%set-element foo bar baz)
;Value: baz
;;; +
(set! (setter element) %original-set-element)
;Value: #[generic function ...]
;; Page 62
(define foo (vector 'a 'b 'c 'd))
;Value: foo
foo
;Value: #(a b c d)
(element foo 2)
;Value: c
(set! (element foo 2) 'sea)
;Value: sea
(element foo 2)
;Value: sea
foo
;Value: #(a b sea d)
;; Page 64
;;; Renamed test to test2, so this definition doesn't conflict with the
;;; previous definition of test.
(define-method test2 ((thing <object>))
(if thing
#t
#f))
;Value: test2
(test2 'hello)
;Value: #t
(test2 #t)
;Value: #t
(test2 #f)
;Value: #f
(define-method double-negative ((num <number>))
(if (< num 0)
(+ num num)
num))
;Value: double-negative
(double-negative 11)
;Value: 11
(double-negative -11)
;Value: -22
;; Page 65
(define-method show-and-tell ((thing <object>))
(if thing
(begin
(print thing)
#t)
#f))
;Value: show-and-tell
(show-and-tell "hello")
;"hello"
;Value: #t
;; Page 65
;;; +
(define-method bonus-illuminated? (pinball post)
#t)
;Value: bonus-illuminated?
;;; +
(define-method add-bonus-score (player delta)
(list 'add-bonus-score player delta))
;Value: add-bonus-score
;;; +
(define current-player 'current-player)
;Value: current-player
;;; +
(define pinball 'pinball)
;Value: pinball
;;; +
(define post 'post)
;Value: post
(when (bonus-illuminated? pinball post)
(add-bonus-score current-player 100000))
;Value: (add-bonus-score current-player 100000)
;; Page 65
;;; +
(define-method detect-gas? (nose)
#f)
;Value: detect-gas?
;;; +
(define-method light (match)
(print (list 'strike match))
(print "KABOOM")
'oh-well)
;Value: light
;;; +
(define nose 'nose)
;Value: nose
;;; +
(define match 'match)
;Value: match
(unless (detect-gas? nose)
(light match))
;(strike match)
;"KABOOM"
;Value: oh-well
;; Page 66
;;; +
(define new-position 100)
;Value: new-position
;;; +
(define old-position 0)
;Value: old-position
(cond ((< new-position old-position)
"the new position is less")
((= new-position old-position)
"the positions are equal")
(else: "the new position is greater"))
;Value: "the new position is greater"
;; Page 67
;;; +
(define-method career-choice (student)
(cond ((id? student 'paul) 'art)
((id? student 'jim) 'history)
((id? student 'steve) 'science)
(else: 'bum)))
;Value: career-choice
;;; Make this a method for easier testing.
(define babble
(method (student)
(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."))))
;Value: babble
;;; +
(babble 'neil)
;Value: "I wish you luck."
;;; +
(babble 'steve)
;"Say, can you fix my VCR?"
;No value
;;; +
(babble 'jim)
;"That really is fascinating."
;No value
;;; +
(babble 'paul)
;"Don't quit your day job."
;No value
;; Page 67
;;; Make this a method for easier testing.
(define whatitis
(method (my-object)
(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"))))
;Value: whatitis
;;; +
(whatitis (make <view>))
;Value: "it's a graphic object"
;;; +
(whatitis #())
;Value: "it's something computational"
;;; +
(whatitis #f)
;;; MIT-Scheme does not distinguish #f from (), so this actually looks like
;;; the end of a list -- "it's something computational".
;Value: "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"
;Value: #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
;Value: factorial
;; Page 69
(define-method first-even ((s <sequence>))
(for-each ((number s))
((even? number) number)
; No body forms needed
))
;Value: first-even
;; Page 70
;;; +
(define-method schedule-game ((city <symbol>) (year <number>))
(print (list year city)))
;Value: schedule-game
(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)))
;Value: schedule-olympic-games
;;; +
(schedule-olympic-games
#(boston new-york baltimore chicago denver san-francisco)
2000)
;(2000 boston)
;(2004 new-york)
;(2008 baltimore)
;(2012 chicago)
;(2016 denver)
;(2020 san-francisco)
;No value
;; Page 70
(begin
(dotimes (i 6) (print "bang!"))
(print "click!"))
;"bang!"
;"bang!"
;"bang!"
;"bang!"
;"bang!"
;"bang!"
;"click!"
;No value
;; Page 71
(define-method first-even ((seq <sequence>))
(bind-exit (exit)
(do (method (item)
(when (even? item)
(exit item)))
seq)))
;Value: first-even
(first-even '(1 3 5 4 7 9 10))
;Value: 4
;; Page 72
+
;Value: #[method ...]
'+
;Value: +
(quote +)
;Value: +
''+
;Value: (quote +)
(+ 10 10)
;Value: 20
'(+ 10 10)
;Value: (+ 10 10)
(quote (+ 10 10))