-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcase-lambda.scm
89 lines (71 loc) · 1.72 KB
/
case-lambda.scm
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
;; case-lambda is being added
;; here are things that happen to work at the moment
;; downgrades to regular lambdas
(define foo (case-lambda (x x)))
(print (foo 1 2 3))
(define foo (case-lambda ((a) a)))
(print (foo "trololo"))
(define foo (case-lambda ((a b c . d) (list a b c d))))
(print (foo 11 22 33 ))
(print (foo 11 22 33 44))
(print (foo 11 22 33 44 55))
;; dispatch, fixed, simple
(define foo
(case-lambda
(() 0)
((a) 1)
((a b) 2)
((a b c) 3)))
(print (foo))
(print (foo 11))
(print (foo 11 22))
(print (foo 11 22 33))
(define foo
(case-lambda
((a) 1)
((a b) 2)
(() 0)
((a b c) 3)))
(print (foo))
(print (foo 11))
(print (foo 11 22))
(print (foo 11 22 33))
;; dispatch w/ variable arity
(define foo
(case-lambda
((a) (list a))
((a) 111) ;; not reachable
((a b . c) (list a b c))
((a b) 222) ;; not reachable
(x x))) ;; not reachable
(print (foo 1))
(print (foo 1 2))
(print (foo 1 2 3))
(print (foo 1 2 3 4))
(print (foo))
;; dispatch, variable arity, literal values, check that their indeces are ok
(define foo
(case-lambda
(() 'zero)
((a) (list 'o 'n 'e))
((a b) 'two) ;; not reachable
((a b c) (cons 'th 'ree))
(x 'any)))
(print (foo)) ;; any
(print (foo 1))
(print (foo 1 2))
(print (foo 1 2 3))
(print (foo 1 2 3 4))
;; TODO ---------------------------------------------------------------------
;; Operator position, should do compile time dispatch
;
; (print ((case-lambda ((a) a) (xs xs)) 1 2 3))
;; Recursion
;
; (define slartibartfast
; (case-lambda
; ((a) a)
; ((a b) (slartibartfast b))
; ((a b . c) (slartibartfast b c))))
;
; (print (slartibartfast 'x 'y 11 22 33 44))