aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/std.lisp
blob: 2a042a0ada0deb2e8bf48c7318b9483174bc5379 (plain)
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
(define (caar ls) (car (car ls)))
(define (caaar ls) (car (caar ls)))
(define (caaaar ls) (car (caaar ls)))
(define (caaaaar ls) (car (caaaar ls)))

(define (cddr ls) (cdr (cdr ls)))
(define (cdddr ls) (cdr (cddr ls)))
(define (cddddr ls) (cdr (cdddr ls)))
(define (cdddddr ls) (cdr (cddddr ls)))

(define (null? ls) (eq? ls '()))

(define (length ls)
  (if (null? ls)
    0
    (+ 1 (length (cdr ls)))))

(define (fold init accumulator ls)
  (if (null? ls)
    init
    (fold (accumulator init (car ls))
          accumulator
          (cdr ls))))

(define (map func ls)
  (if (null? ls)
      '()
      (cons (func (car ls))
            (map func (cdr ls)))))

(define (filter pred ls)
  (if (null? ls)
      '()
      (if (pred (car ls))
          (cons (car ls) (filter pred (cdr ls)))
          (filter pred (cdr ls)))))

(define (member? item ls)
  (fold #f 
        (lambda (acc x) (or acc (eq? item x)))
        ls))

(define (assert expr)
  (assert-eq #t expr))

(define (sum ls) (fold 0 + ls))
(define (product ls) (fold 1 * ls))

(define (enumerate start stop step)
  (if (>= start stop)
      '()
      (cons start
            (enumerate (+ start step) stop step))))