-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.scm
45 lines (34 loc) · 874 Bytes
/
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
(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 (cadar x) (car (cdar x)))
; (define (fold f x xs)
; (if (null? xs) x (fold f (f x (car xs)) (cdr xs))))
(define (reverse-impl xs acc)
(if (null? xs)
acc
(reverse-impl (cdr xs) (cons (car xs) acc))))
(define (reverse xs)
(reverse-native xs))
;(reverse-impl xs '()))
(define (fold-list xs f)
(reverse (fold f '() xs)))
;(define (length xs)
; (fold (lambda (ct x) (+ ct 1)) 0 xs))
(define (append xs ys)
(reverse
(fold
(lambda (acc x) (cons x acc))
(reverse xs)
ys)))
; (define (reverse xs)
; (fold (lambda (tail x) (cons x tail)) '() xs))
(define (map f xs)
(fold-list xs
(lambda (acc x) (cons (f x) acc))))
(define (writeln x)
(write x)
(newline))
(define eof-object? null?)
(define (> a b) (< b a))