Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SCALA Continued Fraction Grammar . We will define continued fractions inductively by the grammar: OneByNumber() | NumPlusContFrac(, ) Here represents the Scala integer number type

SCALA

Continued Fraction Grammar. We will define continued fractions inductively by the grammar:

OneByNumber()

| NumPlusContFrac(, )

Here represents the Scala integer number type (positive/negative whole numbers).

Example 1:

The number will be represented in our grammar by OneByNumber(5).

Example 2:

The number

will be represented in our grammar by

NumPlusContFrac(2,NumPlusContFrac(3,OneByNumber(4)))

USING ABOVE :

Write a function cfrac_of_list that given a list of integers lst of the form List(a0, a1, ..., an), converts it into the continued fraction of depth given by the numbers a0, .., an in the list. Function takes in a single argument whose type must be List[Int]. Return type of your function must be CFrac. If your input is empty your code must throw an IllegalArgumentException.

Example 1:

Input: List(5)

Function returns: OneByNumber(5)

Example 2:

Input: List(2, 3, 4)

Function returns: NumPlusContFrac(2,NumPlusContFrac(3,OneByNumber(4)))

Restrictions:

No loops, var or return.

You can use pattern matching over Scala Lists and any list API functions of your choice.

Your code must throw an IllegalArgumentException if the list is empty.

Your program should pass the following tests:

val g0 = OneByNumber(5)

testWithMessage(cfrac_of_list(List(5)), g0, "Test # 1")

val g1 = NumPlusContFrac(1,NumPlusContFrac(2,OneByNumber(3)))

testWithMessage(cfrac_of_list(List(1,2,3)), g1, "Test # 2")

try {

println("Calling your function cfrac_of_list on empty list.")

val g = cfrac_of_list(Nil);

assert(false, "No IllegalArgumentException thrown on empty list")

} catch {

case e:IllegalArgumentException => {

println("IllegalArgumentException caught: Expected behavior seen!")

}

case e: Exception => {println(s"Wrong type of exception thrown-- $e")}

}

val g3 = NumPlusContFrac(1,

NumPlusContFrac(1,

NumPlusContFrac(1,

NumPlusContFrac(1,

NumPlusContFrac(1,OneByNumber(1))))))

testWithMessage(cfrac_of_list(List(1,1,1,1,1,1)), g3, "Test # 3")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ What is the nature of the contracts or agreements with unions?

Answered: 1 week ago

Question

=+What is the procedure for labor relations in the workplace?

Answered: 1 week ago

Question

=+ Are ballots compulsory?

Answered: 1 week ago