-
Notifications
You must be signed in to change notification settings - Fork 6
/
numbers.rkt
95 lines (81 loc) · 1.97 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
89
90
91
92
93
94
95
#lang racket/base
(provide
my-+
my-+-naturally-recursive
my--
my-x
my->
my-<
my-^
my-=
my-/
one?)
(define my-+
(lambda (n1 n2)
(cond
((zero? n1) n2)
(else (my-+ (sub1 n1) (add1 n2))))))
;; Apparently this is considered bad practice in the little schemer due
;; to not being a 'natural recursion'
;; see: http://stackoverflow.com/questions/32260444/what-is-the-definition-of-natural-recursion
;; and: http://i.stack.imgur.com/QhBcl.png
;; Here's an alternative that is naturally recursive
(define my-+-naturally-recursive
(lambda (n1 n2)
(cond
((zero? n1) n2)
;; Rather than using tail recursion we instead modify a single argument
;; to get closer to the termination condition (see the third commandment)
;;
;; Performing the addition inside the arguments doesn't bring the argument
;; list closer to termination (although it doesn't take it further from
;; termination...)
(else (add1 (my-+-naturally-recursive (sub1 n1) n2))))))
(define my--
;; n1 - n2
(lambda (n1 n2)
(cond
((zero? n2) n1)
(else (sub1 (my-- n1 (sub1 n2)))))))
(define my-x
(lambda (n1 n2)
(cond
((zero? n2) 0)
(else (+ n1 (my-x n1 (sub1 n2)))))))
(define my->
;; n1 > n2
(lambda (n1 n2)
(cond
((zero? n1) #f)
((zero? n2) #t)
(else (my-> (sub1 n1) (sub1 n2))))))
(define my-<
;; n1 < n2
(lambda (n1 n2)
(cond
((and (zero? n1) (zero? n2)) #f)
((zero? n1) #t)
((zero? n2) #f)
(else (my-< (sub1 n1) (sub1 n2))))))
(define my-=
(lambda (n m)
(cond
((zero? n) (zero? m))
(else (my-= (sub1 n) (sub1 m))))))
(define my-^
(lambda (base exponent)
(cond
((zero? exponent) 1)
(else
(* base (my-^ base (sub1 exponent)))))))
(define my-/
(lambda (n m)
(cond
((< n m) 0)
(else (add1 (my-/ (- n m) m))))))
(define one?
(lambda (n)
(eq? n 1)))
(define even?
(lambda (n)
(= (* (/ n 2) 2) n)))