SCALA LAB 1
1. Instructions
Download the files tester.scala and lab.scala (the file where you will put your code) into a directory. Put all of your code inside the Lab object in lab.scala.
To compile your code you can do either of the following:
• From the linux shell (in the directory containing the code): sbt compile compiles the code and sbt run runs the code.
• From the sbt shell (this is faster): compile compiles your code and run compiles and runs your code. Sometimes if you get strange errors, clean can help. It will remove temporary files to allow you to recompile from scratch.
The entry point to the code is already set to be tester.scala, and it will call the code inside the Lab object in lab.scala with test examples. If you have not finished a particular function in lab.scala, you can comment out the code in tester.scala that calls it.
Upload only lab.scala
Question 1. In the Lab object, write a function stringSum whose input parameters are a and b, both of which are integers. If b is not provided, its default value is 0. This function should return a string of the form ”a + b = [their sum is inserted here]”. For example, stringSum(1,2) should return the string ”a + b = 3” and stringSum(1) should return the string ”a + b = 1”.
For this question you must use string interpolation. The body of this function should be 1 line
Question 2. In the Lab object, write a function even whose input parameter is an integer n. The output is a Vector of the first n even numbers. So even(3) should return a Vector of 0,2,4.
Question 3. In the lab object, write a function threshold that takes 3 parameters (all of them are doubles): w, x, b. It should return 1 if w∗x>b and 0 otherwise.
Question 4. In the Lab object, write a function twice that takes in 2 parameter groups. The first parameter is a function fn that takes in a double and outputs a double and the second parameter is a double z. Your function twice should apply fn to z twice (that is, you feed z into fn and you take that result and feed it into fn again).
For example, if fn is the function:
def myFunction(x: Double) = {x*x + 1}
then applying myFunction to 3 will produce 10, and applying myFunction to 10 gives 101. So calling twice(myFunction)(3) should output 101.
1