-
Notifications
You must be signed in to change notification settings - Fork 0
/
domino_loops.scm
140 lines (128 loc) · 3.02 KB
/
domino_loops.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
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
(define domino-loops
(lambda (n)
(filter loop? (permutations (dominoes n)))
)
)
(define filter
(lambda (f L)
; return list of those elements in L which pass through filter f
(if (null? L)
L
(let ((N (f (car L))))
(if (null? N)
(filter f (cdr L))
(cons N (filter f (cdr L)))
)
)
)
)
)
(define dominoes
(lambda (n)
(let (
(ll (build-my-same-list n n))
(lr (build-my-inc-list n))
)
(map cons ll lr)
)
)
)
(define (permutations ls)
(if (null? ls)
'(())
(apply append
(map
(lambda (l)
(insert-perm (car ls) l)
)
(permutations (cdr ls))
)
)
)
)
(define loop?
(lambda (L)
(let*
(
(fl (cond
((= (cdar L) (caadr L) ) (car L))
((= (caar L) (caadr L) ) (cons (cdar L) (caar L)))
((= (caar L) (cdadr L) ) (cons (cdar L) (caar L)))
(else (car L))
)
)
(ll (compare_list_is_loop fl fl (cdr L)) )
)
(if(< (length ll) (length L)) '()
ll
)
)
)
)
(define (compare_list_is_loop fl cl lsl)
(cond
((null? lsl)
(if(equal? (car fl) (cdr cl)) (list fl)
'()
)
)
(else
(cond
((equal? (cdr cl) (caar lsl))
(cons
(car lsl)
(compare_list_is_loop fl (car lsl) (cdr lsl))
)
)
((equal? (cdr cl) (cdar lsl))
(cons
(cons (cdar lsl) (caar lsl))
(compare_list_is_loop fl (cons (cdar lsl) (caar lsl)) (cdr lsl))
)
)
(else '())
)
)
)
)
(define (insert-perm x ls)
(if (null? ls)
(list (list x))
(cons (cons x ls)
(map
(lambda (l)
(cons (car ls) l)
)
(insert-perm x (cdr ls))
)
)
)
)
(define build-same-list
(lambda (n1 n2)
(if(<= n2 0) '()
(append (list n1) (build-same-list n1 (- n2 1)))
)
)
)
(define build-inc-list
(lambda (n1 )
(if(< n1 0 ) '()
(append (list n1) (build-inc-list (- n1 1)))
)
)
)
(define build-my-inc-list
(lambda (n1)
(if(< n1 0 ) '()
(append (build-inc-list n1) (build-my-inc-list (- n1 1)))
)
)
)
(define build-my-same-list
(lambda (n1 n2)
(if(< n1 0 ) '()
(append (build-same-list n1 (+ n1 1)) (build-my-same-list (- n1 1) n2))
)
)
)