-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework_4a.scm
154 lines (138 loc) · 5.75 KB
/
homework_4a.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
;;1. Мемоизация
(define memoized-factorial
(let ((known-results '()))
(lambda (n)
(let* ((arg (list n))
(res (assoc arg known-results)))
(if res
(cadr res)
(let ((res (if (<= n 1)
1
(* n (memoized-factorial (- n 1))))))
(set! known-results (cons (list arg res) known-results))
res))))))
;;2. Отложенные вычисления
(define-syntax lazy-cons
(syntax-rules ()
((_ a b)
(let ((b-promise (delay b)))
(cons a b-promise)))))
(define (lazy-car p)
(car p))
(define (lazy-cdr p)
(force (cdr p)))
(define (lazy-head xs k)
(if (> k 0)
(cons (lazy-car xs) (lazy-head (lazy-cdr xs) (- k 1)))
'()))
(define (lazy-ref xs k)
(if (= k 0)
(car xs)
(lazy-ref (lazy-cdr xs) (- k 1))))
(define (naturals n)
(lazy-cons n (naturals (+ n 1))))
(define (factorials)
(let loop ((p 1) (n 1))
(lazy-cons (* p n) (loop (* p n) (+ n 1)))))
(define (lazy-factorial n)
(car (reverse (lazy-head (factorials) n))))
;;3. Чтение из потока
(define (words-output-from-file way)
(define (loop way res chek)
(let ((x (read-char way)))
(if (eof-object? x)
res
(if (and (equal? chek "") (or (equal? x #\space) (equal? x #\newline)))
(loop way res "")
(if (equal? x #\space)
(loop way (append res (list chek)) "")
(loop way res (string-append chek (string x))))))))
(call-with-input-file way (lambda (port) (write (loop port '() "")))))
;;4. Структуры (записи)
(define __eval__ (lambda (x) (eval x (interaction-environment))))
(define (reversible-reversing-string-to-symbol s)
(or (and (symbol? s) (symbol->string s)) (string->symbol s)))
(define-syntax define-struct
(syntax-rules ()
((_ name (field-value ...))
(begin
(__eval__ (list 'define
(reversible-reversing-string-to-symbol
(string-append "make-" (reversible-reversing-string-to-symbol (quote name))))
(lambda (field-value ...)
(list (list 'type-of-struct (quote name)) (list 'field-value field-value) ...))))
(__eval__ (list 'define
(reversible-reversing-string-to-symbol
(string-append (reversible-reversing-string-to-symbol (quote name)) "?"))
(lambda (x)
(and (pair? x)
(pair? (car x))
(assoc 'type-of-struct x)
(not (null? x))
(equal? (cadr (assoc 'type-of-struct x)) (quote name))))))
(__eval__ (list 'define
(reversible-reversing-string-to-symbol
(string-append
(reversible-reversing-string-to-symbol (quote name))
"-"
(reversible-reversing-string-to-symbol 'field-value)))
(lambda (x)
(cadr (assoc 'field-value (cdr x))))))
...
(__eval__ (list 'define
(reversible-reversing-string-to-symbol
(string-append "set-"
(reversible-reversing-string-to-symbol (quote name))
"-"
(reversible-reversing-string-to-symbol 'field-value)
"!"))
(lambda (x val)
(set-car! (cdr (assoc 'field-value (cdr x))) val)))) ... ))))
;;5. Алгебраические типы данных
(define-syntax define-data
(syntax-rules ()
((_ data-name ((name field-value ...) ...))
(begin
(__eval__ (list 'define
(quote name)
(lambda (field-value ...)
(list (list 'name-of-the-algebraic-data-structure (quote data-name))
(list 'field-name-of-an-algebraic-data-structure (quote name))
(list (quote field-value) field-value)
...))))
...
(__eval__ (list 'define
(reversible-reversing-string-to-symbol
(string-append
(reversible-reversing-string-to-symbol (quote data-name))
"?"))
(lambda (x)
(and (pair? x)
(pair? (car x))
(>= (length x) 2)
(assoc 'name-of-the-algebraic-data-structure x)
(equal? (cadr (assoc 'name-of-the-algebraic-data-structure x))
(quote data-name))))))))))
(define-syntax match
(syntax-rules ()
((_ x ((name field-value ...) expr) ...)
;(and (pair? x) ((set-cdr! (list field-value ...)) ...)))))
(cond ((not (pair? x)) x)
((equal? (cadadr x) (quote name))
(let ((field-value (cadr (assoc (quote field-value) x))) ...)
expr))
...
(else x)))))
;\_______________________________________________λ*\achive/*λ_______________________________________________/
(define-syntax my-let
(syntax-rules ()
((_ ((var val)) . exprs)
((lambda (var) (begin . exprs)) val))
((_ ((var val) . xs) . exprs)
(_ xs ((lambda (var) (begin . exprs)) val)))))
(define-syntax my-let*
(syntax-rules ()
((_ ((var val)) . exprs)
((lambda (var) (begin . exprs)) val))
((_ ((var val) . xs) . exprs)
((lambda (var) (_ xs . exprs)) val))))