-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers.rkt
90 lines (69 loc) · 1.55 KB
/
numbers.rkt
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
#lang racket
(provide x plus pow)
(define (add1 n) (+ n 1))
(define (sub1 n) (- n 1))
(define (plus n m)
(cond
[(zero? m) n]
[else (add1 (plus n (sub1 m)))]))
(define minus
(lambda (n m)
(cond
[(zero? m) n]
[else (sub1 (minus n (sub1 m)))])))
(define add_tuple
(lambda (tuple)
(cond
[(null? tuple) 0]
[else (plus (car tuple) (add_tuple (cdr tuple)))])))
(define x
(lambda (n m)
(cond
[(zero? m) 0]
[else (plus n (x n (sub1 m)))])))
(define tuple+
(lambda (tup1 tup2)
(cond
[(null? tup1) tup2]
[(null? tup2) tup1]
[else (cons (plus (car tup1) (car tup2)) (tuple+ (cdr tup1) (cdr tup2)))])))
(define gt
(lambda (a b)
(cond
[(zero? a) #f]
[(zero? b) #t]
[else (gt (sub1 a) (sub1 b))])))
(define lt
(lambda (a b)
(cond
[(zero? b) #f]
[(zero? a) #t]
[else (lt (sub1 a) (sub1 b))])))
(define equal
(lambda (a b)
(not (or (gt a b) (lt a b)))))
(define pow
(lambda (a b)
(cond
[(zero? b) 1]
[else (x a (pow a (sub1 b)))])))
(define divide
(lambda (a b)
(cond
[(lt a b) 0]
[else (add1 (divide (minus a b) b))])))
(define length
(lambda (lat)
(cond
[(null? lat) 0]
[else (add1 (length (cdr lat)))])))
(define pick
(lambda (n lat)
(cond
[(zero? (sub1 n)) (car lat)]
[else (pick (sub1 n) (cdr lat))])))
(define remove
(lambda (n lat)
(cond
[(zero? (sub1 n)) (cdr lat)]
[else (cons (car lat) (remove (sub1 n) (cdr lat)))])))