Csc 4330/6330, Programming Language Concepts (Summer 2020)

Homework 5 (Due: July 21, Tuesday; Late Submission: July 24, Friday)

Handin under assignment 5
sudo handin4330 5 Roots.scala WordValue.scala HOF.scala Rational.scala Convert.scala

  1. (Roots.scala) Consider the following Scala code to implement fixpoint concept from Scala2.html:
    import scala.math.abs
    
    val tolerance = 0.0001
    
    def isCloseEnough(x: Double, y: Double) =
        abs((x-y)/x) < tolerance
    
    def fixpoint(f: Double => Double)(firstGuess: Double) = {
        def iterate(guess: Double): Double = {
            val next = f(guess)
            println(next)
            if (isCloseEnough(guess, next)) next
            else iterate(next)
        }
        iterate(firstGuess)
    }
    
    def sqrt(x: Double) = fixpoint(y => (y + x/y)/2)(1.0)
    
    sqrt(2)
    
    Write functions to find the roots of the following functions by calling fixpoint with appropriate function parameters and initial values.
    1. x4 - x - 10 = 0
    2. cos(x) - x ex = 0
    The function signatures are as follows:
    def roota = ???
    def rootb = ???
    
    Include the complete solution in the file Roots.scala so that we can run it from command line. The solutions can be extracted from Fixed Point Iteration Method

  2. (WordValue.scala) Write a Scala function, to compute the value of a word. The value of a word is defined as the sum of the values of the individual letters of the word with value of "a" being 1, "b" being 2 etc. Upper and lower case letters have the same value. You can assume that the word consists of just letters. Here is the signature of the function:
    def wordValue(s: String): Int = ???
    
    For example, wordValue("Attitude") = 100. No loops/iterations allowed. Use recursion. Submit just the code for the function and any helpers you develop. We will call your function with our test cases.

  3. (HOF.scala) Define the following two functions:
    // given f and g, compose returns the composition of f with g.
    def compose(f:Int=>Int, g: Int=>Int): Int=>Int = ???
    
    // given f and a positive integer n, repeated returns a function that applies f 
    // to its input n times. i.e. f(f(...(f(x))...))
    def repeated(f:Int=>Int,n:Int): Int=>Int = ???
    
    For example, compose(x=>x*x, x=>x+1)(6) = 49 and repeated(x=>3*x,4)(8) = 648. Submit just the definitions of the two functions in HOF.scala. We will call these with our test cases.

  4. (Rational.scala) Complete the function definitions for creating and manipulating Rational Numbers. You will represent a rational number as a pair of integers in reduced form, i.e. the rational number 3/9 will be reduced to 1/3 and stored as (1,3). Also, if the rational number is positive, neither the numerator nor the denominator should be stored as negative and if the rational number is negative then you will store only the numerator as negative. Use the following templates. You should add additional test cases in Driver.scala. Submit just Rational.scala. We will use our own Driver.scala while grading.

    Rational.scala
    Driver.scala

  5. (Convert.scala) Complete the definitions of the following two functions:
    def convertNum2Binary(num: Int): String =
    
    def convertFraction2Binary(num: Double): String =
    
    Here are sample calls to these functions:
    convertNum2Binary(100) = "1100100"
    convertFraction2Binary(0.375) = ".011"
    convertFraction2Binary(0.8) = ".11001100110011001100110"
    
    In case of repeating patterns, stop at length 23 No loops allowed. Use recursion. Submit just the code for these two functions.