Problem 1. FP Concepts
- Referential transparency.
- Explain informally and with your own words what referential transparency means. It is silly to say this, but do not copy/paste the definition from the book/note/other sources.
- Give an example of a non-trivial expression that is referential transparent. Explain why it is so using the formal textbook definition. (Just to be sure, do not use the examples from the textbook/notes.
Not going to repeat this again)
- Give an example of a non-trivial expression that is not referential transparent. Explain why it is so using the formal textbook definition.
- Function purity.
- Explain informally and with your own words what a pure function is.
- Give an non-trivial example of a pure function written in Scala. Explain why it is so using the formal textbook definition.
- Give an example of a non-trivial impure function written in Scala. Explain why it is so using the formal textbook definition.
- Explain why writing pure functions helps us get these advantages:
- composable code and modularity
- simplified testing
(mention the implications of separation of concerns, made possible by function purity)
Problem 2. The substitution model
a)
a.1 Explain with your own words how the substitution model is used for equational reasoning. a.2 Consider this short program:
object p1 {// format a multiline string with product UPC and namedef addProduct(upc: Int, prodName: String, prevProds: String) : String = { val line = UPC:%04d Product:%20s.format(upc, prodName) prevProds + + line}def main(args: Array[String]): Unit = { val prod0 = Apple 2 val prod1 = IBM PCval productLines0 = addProduct(325, prod0, )val productLines1 = addProduct(103, prod1, productLines0)// non FP code to display the result: println(Products: + productLines1 + ) } |
}
Use the substitution model to derive the value for variable productLines1. a.3 Explain why function addProduct is a pure function.
- Consider this code:
import java.time.LocalDate // a date class in the local timezone// University Registrar class. class Registrar(val univName: String) {private var allStudents = List[Student]() // a MUTABLE instance variable// add student to the list of students registered // Return the number of students already registereddef register(s: Student) : Int = { allStudents = allStudents :+ s// operator :+ appends something at the end of a list and returns the new list allStudents.size}def hasRegistered(s: Student) : Boolean = {@annotation.tailrecdef go(lst: List[Student]) : Boolean = {if (lst == Nil) false else {if (lst.head.name == s.name) true else go(lst.tail)} }go(this.allStudents)}}// A student class. class Student(val name:String) {// putting val in front of parameter makes name an immutable instance variable val startedAt : LocalDate = LocalDate.now()}object p2 { def main(args: Array[String]): Unit = { val univ = new Registrar(FAU) val alice = new Student(Alice) val bob = new Student(Bob)val count1 = univ.register(alice) |
val count2 = univ.register(bob) val didBobRegister = univ.hasRegistered(bob)println(%d students registered.format(count2))println(Did Bob register? %b.
.format(didBobRegister)) }} |
- Is expression register(bob) referential transparent ? Explain your answer.
- Is expression hasRegistered(bob) referential transparent ? Explain your answer.
- for 3 points extra credit extra credit extra credit extra credit extra credit extra credit Sketch a solution for the student registration part that is pure, i.e. preserves referential transparency. It
does not have to be a complete, working program. Use the methodology explained in the textbook/lecture. No need to write Scala code in a separate file.
Problem 3. Recursion and HOFs
Write the code for this problem in a file called p3.scala, within an object called p3. a) Consider this function written in a non-functional, imperative style:
// imperative, impure version: dont do like thisdef trueForAll_imperative(bools: List[Boolean]) : Boolean = { var lst = bools while (lst != Nil) { if (! lst.head) {return false // CAUTION: non-functional, bad programming style to use return} else {lst = lst.tail} } true} |
Write a tail recursive pure function called trueForAll that returns the same values as trueForAll_imperative. Use the correct annotation.
- Write a tail-recursive polymorphic function with this signature: def countWithProperty[T](xs: List[T], p: T => Boolean) : Int that returns the number of elements x from list xs for which p(x) is true.
For example:
val luckyNumbers = List(4, 8, 15, 16, 23, 42)
val evenCount = countWithProperty(luckyNumbers, (x: Int) => x % 2 == 0)
The value of variable evenCount is 4.
- Write a tail-recursive polymorphic function with this signature: def scalarProduct(x: Array[Double], y: Array[Double]) : Double that returns the scalar product of arrays x and y.
For example:
val xvec = Array(-1.0, -2, 3) val yvec = Array(2.0, -3, 1)val prod = scalarProduct(xvec, yvec) // prod == 7 |
- Write a tail-recursive polymorphic function with this signature: def countCommonElements[T](xs: Array[T], ys: Array[T], p: (T, T) => Boolean): Int
that returns the number of elements x in xs and y in ys, such that p(x, y) is true. This can be used, for example, to count the number of elements in xs that are also in ys:
val as = Array(7, 3, 8, 4, 9, 3, 5) val bs = Array(8, -4, 5, 1, 0, 2, 4)val commonNumbers = countCommonElements(as, bs, (x: Int, y: Int) => x == y) // commonNumbers == 3, for 8, 5, 4 are both in as and bs |
- Write a main function in object p3 that illustrates how to use the above functions with anonymous functions. Do not use the example code given in parts a)-d).
Problem 4. Partial application, composition, currying.
Write the code for this problem in a file called p4.scala, within an object called p4. Add in p4 an empty method main.
- Partial function application.
- Write a function partial2 that takes as parameters a value a:A and a function f:(A,B,C)=>D, of 3 arguments, and returns as result a function of two arguments that partially applies f to This is an extension of the textbook function partial1 from 2 arguments to 3 arguments.
- Consider this function of three arguments:
def formatStudentInfo(isGraduate: Boolean, year: Int, name: String) = { val grad = if (isGraduate) graduate else undergraduate %s student %s enrolled in year %d.format(grad, name, year) }
Write code in function main that shows how to obtain a function of 2 parameters using partial2 that partially applies formatStudentInfo with parameter isGraduate set to true. Assign the new function to a val called formatGraduateStd. Then write in main code that uses formatGraduateStd with some sample student information and display the resulting string.
(Hint: if this sounds complicated, the problem wants to use partial2 in the same way the lecture notes show how to use partial1 with a function of 2 arguments called firstNChars.) b) Composition.
- Consider these functions that format HTML elements:
def htmlTag(elem: String, inner: String) = <%s>%s</s>n
.format(elem, inner, elem) val mkBold = (inner: String) => htmlTag(B, inner)
Define in object p4s main method an immutable variable called mkItalic initialized with a lambda expression that is similar to mkBold but instead of returning a boldface <B></B> element (like mkBold) it returns an italic face element, <I>.</I>.
- Define in object p4s main method an immutable variable called mkBoldItalic initialized with a lambda expression that uses the compose method from trait Function2 and the two lambda expressions, mkBold and mkItalic to format an italic bold font style with the composed element <I><B>.<B></I>.
- Define in object p4s main method an immutable variable called mkItalicBold initialized with a lambda expression that uses the andThen method from trait Function2 and the two lambda expressions, mkBold and mkItalic to format a bold italic font style with the composed element <B><I>.</I></B>. c) Currying.
- Write a function curry2 that takes as parameters a function f:(A,B,C)=>D, of 3 arguments, and returns as result a function of type A that returns a function of B and C returning D, e. curry2 returns type A => ((B,C) => D).
This is an extension of the textbook function curry from argument functions with 2 parameters, i.e.
from (A, B) => C to 3 parameters, (A,B,C)=>D.
In other words, if fs type is (A,B,C) => D, then curry2(f) returns a function of type A => ((B, C) => D). The function returned from curry2(f) takes an a:A argument and returns a function that partially applies f to a.
Hint: use curry()s source code given in the lecture notes as a template.
- Use the curry2 and formatStudentInfo functions to write in p4s main method a lambda expression stored in variable mkStdInfo that can be used to partially apply formatStudentInfo with a Boolean parameter, as shown next:
val mkStdInfo = curry2(formatStudentInfo) : Boolean => (Int, String) => String val formatGradStdInfo = mkStdInfo(true) // good for graduate student info
println(formatGradStdInfo(2020, Harry Potter))
// displays: graduate student Harry Potter enrolled in year 2020
- Use lambda expression mkStdInfo to write in p4s main method a lambda expression stored in variable formatUndergradStdInfo that can be used to format data for undergraduate students in the same way formatGradStdInfo is used for graduate students. Problem 5. Traits, objects, and classes.
Write the code for this problem in a file called p5.scala, within an object called p5. Add in p5 an empty method main.
- Write a trait called Shape with two methods returning Double: area and perimeter.
- Write a class called Circle with the appropriate instance variables that extends Shape.
- Write a class called Rectangle with the appropriate instance variables that extends Shape.
- Write code in main variables that uses these classes.
- Initialize in main a variable (val) with an expression that returns an anonymous class implementing Shape that returns a perimeter of value 16.0 and an area 15.0.
Reviews
There are no reviews yet.