Question
SCALA sealed trait CFrac case class OneByNumber(num: Int) extends CFrac case class NumPlusContFrac(num: Int, contFrac: CFrac) extends CFrac case class Frac(num: Int, contFrac: CFrac) extends
SCALA
sealed trait CFrac case class OneByNumber(num: Int) extends CFrac case class NumPlusContFrac(num: Int, contFrac: CFrac) extends CFrac case class Frac(num: Int, contFrac: CFrac) extends CFrac USING ABOVE:
SHOW THAT IT PASSES ALL TESTs:
Implement a function double_of_cfrac that takes in a continued fraction expressed using the inductive definition above and evaluates the corresponding double precision number.
Restrictions:
You are required to use case pattern matching.
Your function need not be tail recursive.
Forbidden: use of loops, mutable (vars), return statement.
Your program should pass the following tests:
val g1 = NumPlusContFrac(2,NumPlusContFrac(3,OneByNumber(4)))
testWithMessage(double_of_cfrac(g1), 0.43333333, " # 1")
val g2 = OneByNumber(2)
testWithMessage(double_of_cfrac(g2), 0.5, " # 2")
val g3 = NumPlusContFrac(3, NumPlusContFrac(-1, OneByNumber(5)))
testWithMessage(double_of_cfrac(g3), 0.57142857, " # 3")
val g4 = NumPlusContFrac(1, OneByNumber(-2))
testWithMessage(double_of_cfrac(g4), 2.0, " # 4")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started