-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresistors.scm
119 lines (107 loc) · 3.41 KB
/
resistors.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
;;;; See LICENSE file for details
;;;; resistors.scm is part of the greed project, a collection of Scheme utilities.
;(load "list-utils.scm")
;; acceptable color bands
(define acceptable-colors
(list "blk" "brn" "red" "orn" "yel" "gre" "blu" "vio" "gol" "sil" "gra" "whi"))
(define valid-color?
(lambda (clr)
(cond ((exists-in? clr acceptable-colors) #t)
(else #f))))
(define temp-coef-color
(lambda (clr)
(cond ((not (valid-color? clr))
(display "temp-coef-color: color not found\n"))
((equal? clr "brn") '100ppm)
((equal? clr "red") '50ppm)
((equal? clr "orn") '15ppm)
((equal? clr "yel") '25ppm)
((equal? clr "blu") '10ppm)
(else '5ppm)))) ; it's violet
(define tolerance-color
(lambda (clr)
(cond ((not (valid-color? clr))
(display "tolerance-color: color not found\n"))
((equal? clr "brn") '1%)
((equal? clr "red") '2%)
((equal? clr "gre") '0.5%)
((equal? clr "blu") '0.25%)
((equal? clr "vio") '0.1%)
((equal? clr "gol") '5%)
(else '10%)))) ; it's silver
(define multiplier-color
(lambda (clr)
(cond ((not (valid-color? clr))
(display "multiplier-color: color not found"))
((equal? clr "blk") 1)
((equal? clr "brn") 10)
((equal? clr "red") 100)
((equal? clr "orn") 1000)
((equal? clr "yel") 10000)
((equal? clr "gre") 100000)
((equal? clr "blu") 1000000)
((equal? clr "vio") 10000000)
((equal? clr "gol") 0.1)
(else 0.01)))) ; it's silver
(define first-bands
(lambda (clr)
(cond ((not (valid-color? clr))
(display "first-bands: color not found\n"))
((equal? clr "blk") 0)
((equal? clr "brn") 1)
((equal? clr "red") 2)
((equal? clr "orn") 3)
((equal? clr "yel") 4)
((equal? clr "gre") 5)
((equal? clr "blu") 6)
((equal? clr "vio") 7)
((equal? clr "gra") 8)
(else 9)))) ; it's white
;; usage example:
;; (colors-to-resistance (list "red" "blk" "red")) --> 2000 Ohm, tolerace: 20%
(define colors-to-resistance
(lambda (lst)
(cond ((null? lst) (display "null list\n"))
((> (length lst) 6)
(display "resistors have max 6 bands, try again\n"))
((eq? (length lst) 3)
(let ((res (* (expt 10 (first-bands (caddr lst)))
(+ (* (first-bands (car lst)) 10)
(first-bands (cadr lst)))))
(tol '20%))
(begin (display res)
(display " Ohm, tolerance: ")
(display tol)
(newline))))
((eq? (length lst) 4)
(let ((res (* (+ (* 10 (first-bands (car lst)))
(first-bands (nth 2 lst)))
(expt 10 (first-bands (nth 3 lst)))))
(tol (tolerance-color (last lst))))
(begin (display res)
(display " Ohm, tolerance: ")
(display tol)
(newline))))
((eq? (length lst) 5)
(let ((res (* (+ (* 100 (first-bands (car lst)))
(* 10 (first-bands (nth 2 lst)))
(first-bands (nth 3 lst)))
(expt 10 (first-bands (nth 4 lst)))))
(tol (tolerance-color (last lst))))
(begin (display res)
(display " Ohm, tolerance: ")
(display tol)
(newline))))
(else (let ((res (* (+ (* 100 (first-bands (car lst)))
(* 10 (first-bands (nth 2 lst)))
(first-bands (nth 3 lst)))
(expt 10 (first-bands (nth 4 lst)))))
(tol (tolerance-color (nth 5 lst)))
(coef (temp-coef-color (last lst))))
(begin (display res)
(display " Ohm, tolerance: ")
(display tol)
(display " temperature coefficient: ")
(display coef)
(newline)))))))
(format #t "; resistors loaded successfully.~%")