-
Notifications
You must be signed in to change notification settings - Fork 1
/
builtin.scm
42 lines (34 loc) · 1.04 KB
/
builtin.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
(define (not a) (if a '#f '#t))
(define (<= a b) (not (< b a)))
(define (> a b) (< b a))
(define (>= a b) (<= b a))
(define (append list1 list2)
(cond
((null? list1) list2)
(#t (cons (car list1) (append (cdr list1) list2)))))
(define (reverse lst)
(if (null? lst)
lst
(append (reverse (cdr lst)) (list (car lst)))))
(define (subsequence lst begin end)
(cond
((or (null? lst) (<= end 0)) '())
((> begin 0) (subsequence (cdr lst) (- begin 1) (- end 1)))
(#t (cons (car lst) (subsequence (cdr lst) 0 (- end 1))))))
(define (map func lst)
(if (null? lst)
'()
(cons (func (car lst)) (map func (cdr lst)))))
(define (fold func acc lst)
(if (null? lst)
acc
(fold func (func acc (car lst)) (cdr lst))))
(define (length lst)
(fold (lambda (acc element) (+ acc 1)) 0 lst))
(define (filter predicate lst)
(if (null? lst)
'()
(if (predicate (car lst))
(cons (car lst) (filter predicate (cdr lst)))
(filter predicate (cdr lst)))))
(define (sum lst) (fold + 0 lst))