-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.scm
executable file
·64 lines (60 loc) · 2.29 KB
/
lists.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
(define caar (lambda (x) (car (car x))))
(define cadr (lambda (x) (car (cdr x))))
(define cdar (lambda (x) (cdr (car x))))
(define cddr (lambda (x) (cdr (cdr x))))
(define caaar (lambda (x) (car (caar x))))
(define caadr (lambda (x) (car (cadr x))))
(define cadar (lambda (x) (car (cdar x))))
(define caddr (lambda (x) (car (cddr x))))
(define cdaar (lambda (x) (cdr (caar x))))
(define cdadr (lambda (x) (cdr (cadr x))))
(define cddar (lambda (x) (cdr (cdar x))))
(define cdddr (lambda (x) (cdr (cddr x))))
(define caaaar (lambda (x) (car (caaar x))))
(define caaadr (lambda (x) (car (caadr x))))
(define caadar (lambda (x) (car (cadar x))))
(define caaddr (lambda (x) (car (caddr x))))
(define cadaar (lambda (x) (car (cdaar x))))
(define cadadr (lambda (x) (car (cdadr x))))
(define caddar (lambda (x) (car (cddar x))))
(define cadddr (lambda (x) (car (cdddr x))))
(define cdaaar (lambda (x) (cdr (caaar x))))
(define cdaadr (lambda (x) (cdr (caadr x))))
(define cdadar (lambda (x) (cdr (cadar x))))
(define cdaddr (lambda (x) (cdr (caddr x))))
(define cddaar (lambda (x) (cdr (cdaar x))))
(define cddadr (lambda (x) (cdr (cdadr x))))
(define cdddar (lambda (x) (cdr (cddar x))))
(define cddddr (lambda (x) (cdr (cdddr x))))
; Defining list is unnecessary since we have a primitive implemented
(define list? (lambda (x)
(and (pair? x)
(or (null? x) (list? (cdr x))))))
(define length (lambda (x)
(if (null? x)
x
(+ 1 (length (cdr x))))))
(define list-ref (lambda (lst x)
(if (null? lst)
(error "Index out of bounds")
(if (zero? x)
(car lst)
(list-ref (cdr lst) (+ x -1))))))
(define list-tail (lambda (lst x)
(if (zero? x)
lst
(if (null? lst)
(error "Index out of bounds")
(list-tail (cdr lst) (+ x -1))))))
(define member
(lambda (x lst)
(if (null? lst)
#f
(if (equal? x (car lst))
lst
(member x (cdr lst))))))
(define assq
(lambda (index lst)
(if (zero? index)
car(lst)
(assq((- index 1) cdr(lst))))))