-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumbers
More file actions
84 lines (72 loc) · 2.42 KB
/
Copy pathNumbers
File metadata and controls
84 lines (72 loc) · 2.42 KB
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
#lang racket
;;;;;; Numbers to digits, vice versa;;;;;;;;;;;;;;;
;;;;;;;Call by (spellNum 123)
;;;;;;;Call by (numberIt '(one two three))
;;;;;;;Call by (addEm 123 456)
;;;;;;;Call by (spellNum '(one two three) '(four five six))
;;Numeric to Alpha Function
(define (spellNum x)
(cond
((zero? x) '())
((= (digit x) 0) (append (spellNum (floor (/ x 10))) '(zero)))
((= (digit x) 1) (append (spellNum (floor (/ x 10))) '(one)))
((= (digit x) 2) (append (spellNum (floor (/ x 10))) '(two)))
((= (digit x) 3) (append (spellNum (floor (/ x 10))) '(three)))
((= (digit x) 4) (append (spellNum (floor (/ x 10))) '(four)))
((= (digit x) 5) (append (spellNum (floor (/ x 10))) '(five)))
((= (digit x) 6) (append (spellNum (floor (/ x 10))) '(six)))
((= (digit x) 7) (append (spellNum (floor (/ x 10))) '(seven)))
((= (digit x) 8) (append (spellNum (floor (/ x 10))) '(eight)))
((= (digit x) 9) (append (spellNum (floor (/ x 10))) '(nine)))
)
)
;;Get each digit
(define (digit x)
(remainder x 10))
;;Alpha to Numeric Function
(define (numberIt L1)
(cond
((null? L1) '())
((eqv? (car L1) `zero) (cons '0 (numberIt (cdr L1))))
((eqv? (car L1) `one) (cons '1 (numberIt (cdr L1))))
((eqv? (car L1) `two) (cons '2 (numberIt (cdr L1))))
((eqv? (car L1) `three) (cons '3 (numberIt (cdr L1))))
((eqv? (car L1) `four) (cons '4 (numberIt (cdr L1))))
((eqv? (car L1) `five) (cons '5 (numberIt (cdr L1))))
((eqv? (car L1) `six) (cons '6 (numberIt (cdr L1))))
((eqv? (car L1) `seven) (cons '7 (numberIt (cdr L1))))
((eqv? (car L1) `eight) (cons '8 (numberIt (cdr L1))))
((eqv? (car L1) `nine) (cons '9 (numberIt (cdr L1))))
)
)
;;Adds number list, returns number
(define (adder L1)
(cond
((null? L1) 0)
((+ (* (car L1) (tens(- (length L1) 1))) (adder(cdr L1))))
)
)
;;Multiply digit to get power of ten
(define (tens x)
(cond
((zero? x) 1)
((* 10 (tens (- x 1))))
)
)
;;Add Function
(define (addEm N1 N2)
(cond
;;If both inputs are just numbers
((and (number? N1) (number? N2))
(+ N1 N2))
;;If the first input is a list
((and(not(number? N1)) (number? N2))
(+ (adder(numberIt N1)) N2))
;;If the second input is a list
((and(number? N1) (not(number? N2)))
(+ N1 (adder(numberIt N2))))
;;If both inputs are a list
((and(not(number? N2)) (not(number? N2)))
(spellNum(+ (adder(numberIt N1)) (adder(numberIt N2)))))
)
)