Solve in Standard Meta Language of New Jersey (SML) and use only recursive defin
ID: 3748314 • Letter: S
Question
Solve in Standard Meta Language of New Jersey (SML) and use only recursive definitions built up from the basic built-in functions!
Write a function parallelResistors that calculates the total resistance of a number of resistors connected in parallel. The formula for computing this is: = 1 /(1/ 1 + 1/ 2 + + 1/ ) where r1, . . . rn are the resistances of each resistor. For example, parallelResistors [10.0, 10.0], representing two 10.0 Ohm resistors in parallel, should return 5.0. Don’t worry about the edge cases of zero Ohm resistors, or no resistors (i.e. an empty list as an argument to parallelResistors). However, after you’ve written your function, try it on these cases and see what happens. Explain why you get the results that you do in a comment . The type of parallelResistors should be real list -> real. Examples:
> parallelResistors [10.0, 10.0, 10.0, 10.0]
2.5
> parallelResistors [8.0, 16.0, 4.0, 16.0]
2.0
> parallelResistors [5.0, 10.0, 2.0]
1.25
Explanation / Answer
fun parallelResistors [] = 0.0
|parallelResistors (x) =
let
fun inverseSum [] = 0.0
| inverseSum (y::ys) =
((1.0/y) + (inverseSum ys))
in
(1.0/(inverseSum x))
end;
fun parallelResistorsTest () =
let
val parallelResistorsT1 = Real.==((parallelResistors [10.0, 10.0, 10.0, 10.0]), 2.5)
val parallelResistorsT2 = Real.==((parallelResistors [8.0, 16.0, 4.0, 16.0]), 2.0)
val parallelResistorsT3 = Real.==((parallelResistors [5.0, 10.0, 2.0]) , 1.25)
in
print ("parallelResistors:-------------------- " ^ " test1: " ^
Bool.toString(parallelResistorsT1) ^ " " ^ " test2: " ^
Bool.toString(parallelResistorsT2) ^ " " ^ " test3: " ^
Bool.toString(parallelResistorsT3) ^ " ")
end;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.