CS205: Scalable Software Systems Lecture 3 September 5, 2016 Lecture 3 CS205: Scalable Software Systems September 5, 2016 1 / 19
Table of contents 1 Quick Recap 2 Type of recursive solutions 3 Translating our solutions to programs 4 Scala hands-on 5 More exercises 6 Further readings and practice questions Lecture 3 CS205: Scalable Software Systems September 5, 2016 2 / 19
Quick Recap Recursive approach of solving the problem Factorial List reversal, concatenation Tower of Hanoi Split a list in two equal parts Check a list of characters for palindrome Insert an integer in a sorted list Bubble up the largest integer at the end Position an integer to its rightful position in a list Lecture 3 CS205: Scalable Software Systems September 5, 2016 3 / 19
Type of recursive solutions Tail recursive vs. Non-Tail recursive Tail recursive A function is called tail recursive if no operation is performed after a value is returned from the call of this function. Lecture 3 CS205: Scalable Software Systems September 5, 2016 4 / 19
Type of recursive solutions Tail recursive vs Non-Tail recursive fact: Int => Int 1 if n = 0 fact(n : Int) = n fact(n 1) otherwise Lecture 3 CS205: Scalable Software Systems September 5, 2016 5 / 19
Type of recursive solutions Factorial: Conversion to a tail-recursive variant fact: Int => Int result if n = 0 facttr(n : Int, result : Int) = facttr(n 1, result n) otherwise Lecture 3 CS205: Scalable Software Systems September 5, 2016 6 / 19
Type of recursive solutions Palindrom checker: Tail recursive or non-tail recursive ispalindrome: List[T] => Bool True if l = [ ] or l = hd :: [ ] False ispalindrome(l : List[T ]) = ispalindrome(mid) if l = hd :: mid :: last and hd last if l = hd :: mid :: last and hd = last Lecture 3 CS205: Scalable Software Systems September 5, 2016 7 / 19
Type of recursive solutions More exercises: Create Tail recursive solutions 1 List length odd or even 2 Split a list in two equal parts 3 Remove all elements in the odd position of a list 4 Reverse a list 5 Insert an element in a sorted list 6 Find the k th element of a list 7 Operations over polynomials of one variables reprsented by a list Lecture 3 CS205: Scalable Software Systems September 5, 2016 8 / 19
Translating our solutions to programs Translating our solutions to a program What all do we need? 1 how to define a function? 2 how to call a function? 3 how to write boolean expressions using variables and constants? 4 how to represent lists? 5 how to return a value from a function? 6 how to do a case analysis? Lecture 3 CS205: Scalable Software Systems September 5, 2016 9 / 19
Translating our solutions to programs Translating our solutions to programs def count ( lst : List [ Int ]): Int = { lst match { case Nil => 0 case l:: tail => ( count ( tail )+1)%2 } } Lecture 3 CS205: Scalable Software Systems September 5, 2016 10 / 19
Translating our solutions to programs Recap: number of elements, odd or even countoddeven: List[Int] => Int 0 if lst = [ ] countoddeven(lst : List[Int]) = 1 + countoddeven(rest) if lst = hd :: rest Lecture 3 CS205: Scalable Software Systems September 5, 2016 11 / 19
Translating our solutions to programs Translating our solutions to programs def reverse ( lst : List [ Int ]): List [ Int ] = { lst match { case Nil = > Nil case l:: tail => reverse ( tail ) ::: List (l) } } Lecture 3 CS205: Scalable Software Systems September 5, 2016 12 / 19
Translating our solutions to programs Recap: list reversal reverse: List[Int] => List[Int] [ ] if lst = [ ] reverse(lst : List[Int]) = reverse(rest) :: hd if lst = hd :: rest Lecture 3 CS205: Scalable Software Systems September 5, 2016 13 / 19
Translating our solutions to programs Combined program with the main function object reversal { def main ( args : Array [ String ]): Unit = { def reverse ( lst : List [ Int ]): List [ Int ] = { lst match { case Nil = > Nil case l:: tail => reverse ( tail ) ::: List (l) } } Console. println (" Reversal of "+ List (1,2,3,4) + " is " + reverse ( List (1,2,3,4))) } } Lecture 3 CS205: Scalable Software Systems September 5, 2016 14 / 19
Translating our solutions to programs Running the program How to compile/run a program? $ scalac filename.scala [compiles the code present in the file filename.scala] $ scala reversal [Executes the program by invoking the main function present in the object reversal] Lecture 3 CS205: Scalable Software Systems September 5, 2016 15 / 19
Scala hands-on Scala hands-on 1 scala installation: http://www.scala-lang.org/download/ 2 scala doc: http://www.scala-lang.org/api/current/#package 3 scalac: compiler 4 scala: runtime comamnd 5 sbt: scala build tool Lecture 3 CS205: Scalable Software Systems September 5, 2016 16 / 19
Scala hands-on Higher order functions and currying Functions are variables and they can be passed around like variables Functions can be composed together: currying Lecture 3 CS205: Scalable Software Systems September 5, 2016 17 / 19
More exercises More exercises Sum of integers in a list Sum of square of integers in a list Sum of cubes of integers in a list Lecture 3 CS205: Scalable Software Systems September 5, 2016 18 / 19
More exercises More exercises Sum of integers in a list Sum of square of integers in a list Sum of cubes of integers in a list Multiplication of integers in a list Lecture 3 CS205: Scalable Software Systems September 5, 2016 18 / 19
More exercises More exercises Sum of integers in a list Sum of square of integers in a list Sum of cubes of integers in a list Multiplication of integers in a list Get a sublist with all numbers greater than 2 Get a sublist with all numbers greater than 5 Get a sublist with all numbers greater than 10 Lecture 3 CS205: Scalable Software Systems September 5, 2016 18 / 19
More exercises More exercises Sum of integers in a list Sum of square of integers in a list Sum of cubes of integers in a list Multiplication of integers in a list Get a sublist with all numbers greater than 2 Get a sublist with all numbers greater than 5 Get a sublist with all numbers greater than 10 Get a sublist with all numbers less than 3 Lecture 3 CS205: Scalable Software Systems September 5, 2016 18 / 19
Further readings and practice questions Readings scala: https://www.cs.helsinki.fi/u/wikla/ots/sisalto/ examples/index.html https://twitter.github.io/scala_school/ SBT: http://www.scala-sbt.org/download.html More questions to solve using the recursive thinking: http://aperiodic.net/phil/scala/s-99/ Lecture 3 CS205: Scalable Software Systems September 5, 2016 19 / 19