-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.scm
102 lines (78 loc) · 2.37 KB
/
stdlib.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
;;; bs library procedures
(define number? integer?)
(define (map proc lst)
(if (null? lst)
'()
(cons (proc (car lst))
(map proc (cdr lst)))))
(define (for-each proc lst)
(if (null? lst)
'ok
(begin
(proc (car lst))
(for-each proc (cdr lst)))))
(define (fold-right proc init lst)
(if (null? lst)
init
(proc (car lst)
(fold-right proc init (cdr lst)))))
(define (fold-left proc init lst)
(define (iter result rest)
(if (null? rest)
result
(iter (proc result (car rest))
(cdr rest))))
(iter init lst))
(define (reverse lst)
(fold-left (lambda (i j)
(cons j i))
'()
lst))
(define (append l1 l2)
(fold-right (lambda (i j)
(cons i j))
l2
l1))
(define (not x)
(if x #f #t))
(define (newline)
(display #\newline))
(define (call-with-input-file file proc)
(let ((port (open-input-file file)))
(let ((result (proc port)))
(close-input-port port)
result)))
(define (call-with-output-file file proc)
(let ((port (open-output-file file)))
(let ((result (proc port)))
(close-output-port port)
result)))
(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))
(define (cdar x) (cdr (car x)))
(define (cddr x) (cdr (cdr x)))
(define (caaar x) (car (car (car x))))
(define (caadr x) (car (car (cdr x))))
(define (cadar x) (car (cdr (car x))))
(define (caddr x) (car (cdr (cdr x))))
(define (cdaar x) (cdr (car (car x))))
(define (cdadr x) (cdr (car (cdr x))))
(define (cddar x) (cdr (cdr (car x))))
(define (cdddr x) (cdr (cdr (cdr x))))
(define (caaaar x) (car (car (car (car x)))))
(define (caaadr x) (car (car (car (cdr x)))))
(define (caadar x) (car (car (cdr (car x)))))
(define (caaddr x) (car (car (cdr (cdr x)))))
(define (cadaar x) (car (cdr (car (car x)))))
(define (cadadr x) (car (cdr (car (cdr x)))))
(define (caddar x) (car (cdr (cdr (car x)))))
(define (cadddr x) (car (cdr (cdr (cdr x)))))
(define (cdaaar x) (cdr (car (car (car x)))))
(define (cdaadr x) (cdr (car (car (cdr x)))))
(define (cdadar x) (cdr (car (cdr (car x)))))
(define (cdaddr x) (cdr (car (cdr (cdr x)))))
(define (cddaar x) (cdr (cdr (car (car x)))))
(define (cddadr x) (cdr (cdr (car (cdr x)))))
(define (cdddar x) (cdr (cdr (cdr (car x)))))
(define (cddddr x) (cdr (cdr (cdr (cdr x)))))
'stdlib-loaded