Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Week39, DM509 Fall 2013, Weekly Notes September 26, 2013 Monday, Lecture We will start with a broad introduction of Scala and functional programming. Then we will look at some examples followed by the introduction of the terms mutable and immutable. Then, I will show you some of the basic language constructs in Scala and some strategies for the evaluation of expressions. Reading Scala notes chapters 1, 2, and sections 3.1, 3.2, 3.3, and 3.4. Tuesday, Lecture We pick up where we stopped Monday and continue with precedence of operators. Then, we will introduce pattern matching. After this, we will introduce algebraic data types for natural numbers and lists. We will then talk about generic types after which we will introduce function values and possibly also higher order functions. Reading Scala notes 3.5, 3.6, 3.7, 3.8, 3.9 Thursday, Exercise Please show up in IMADAs terminal room for this exercise session. If arrays are partial functions, i.e. arrays of length 6 are functions f : Int → 7 Int defined for i ∈ [0, 5], we can make function composition from arrays. 1. Given the following program: val x = Array(1,4,0,2,5,3) val y = Array() val f = x compose y //Fill out 1 (a) Fill out y with integers such that f (i ) = i , i.e. f is the identity function in the range [0, 5]. For a sequence, xs, we define map(λ, xs) := [λ(xs0 ), ..., λ(xsn−1 )] Thus , in Scala, you can test your result as such: val domain = List.range(0,6) domain == (domain map f) (b) Continuing with your program, in the following code, fill out z to make g yet another identity: val z = Array() val g = x compose x compose z //Fill out Use the same approach as before to test your new identity, g. 2. The Fibonacci Sequence is defined by F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 . Make a recursive function, def fib(n:Int):Int that naively calculates the nth fibonacci number. Test your function with the following code 0 to 43 map fib. Does your code take a long time to execute? 3. Using the following algebraic data type for lists of integers: abstract class List case class Cons(head:Int,tail:List) extends List case object Nil extends List Using pattern matching, define the following functions: (a) def head(xs:List):Int (b) def tail(xs:List):List (c) def isEmpty(xs:List):Boolean Get a piece of paper and write down a mathematical pseudo-code implementation for a recursive function, last, such that last(xs) is the last element of a list xs. (a) Is your function defined for empty lists? (b) Write down a proof-by induction that your implementation works, complete with base-case, induction hypothesis and inductive step. (c) Finally, implement your (provably correct!) function def last(xs:List):Int in Scala using pattern matching. (d) Test your code with the following code: val xs = Cons(42,Cons(1337,Cons(9000,Nil))) val ys = Cons(1,Cons(2,Cons(4,Cons(8,Nil)))) println(last(xs)) println(last(ys)) 2 Friday, Exercise We continue with the 2 exercises from last week (clausal form + SLD-trees). After this, we solve these problems: 1. Consider the following two definitions: def f( x:=>Int, y:Int )= x + x def g( x:Int, y:=>Int )= x + x + y. For the following calls, which takes fewer steps to evaluate? (a) f(10,5+5) or g(10,5+5)? (b) f(5+5,2) or g(1+2+3,10+10)? (c) f(5+5,10*100) or g(1+2,3) 2. Using the following algebraic data type: abstract class List case class Cons(head:Int,tail:List) extends List case object Nil extends List (a) Write a mathematical formulation of a recursive function sum(xs) that calculates the sum of the list xs. (b) By proof of induction, show that your function is correct. (c) Translate your function into a Scala function def sum(xs:List):Int 3. Implement and test a function, def length(xs:List):Int, that finds the length of a list (without looking at slides+lecture notes!). 4. (a) Write a mathematical formulation of a recursive function klast(xs,k) that finds the k th-last element of the list xs, e.g. for a list ys = [1, 2, 3, 4, 5], klast(ys,4) == 2 and klast(ys,2) == 4. (b) By proof of induction, show that your function is correct. (c) Translate your function into a Scala function def klast(xs:List,k:Int):Int 3