CS205: Scalable Software Systems

Similar documents
CS205: Scalable Software Systems

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

CS558 Programming Languages

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

C22a: Problem Solving using Recursion

CS558 Programming Languages

CSC 1351: Quiz 6: Sort and Search

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

CS103L SPRING 2017 UNIT 8: RECURSION

CS558 Programming Languages

Mini-ML. CS 502 Lecture 2 8/28/08

G Programming Languages - Fall 2012

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Lecture 18: Lists II. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Programming Languages and Techniques (CIS120)


q To develop recursive methods for recursive mathematical functions ( ).

q To develop recursive methods for recursive mathematical functions ( ).

Pace University. Fundamental Concepts of CS121 1

CS1 Lecture 15 Feb. 18, 2019

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Recursion. Recursion is: Recursion splits a problem:

Shell CSCE 314 TAMU. Higher Order Functions

Classical Themes of Computer Science

Lecture: Functional Programming

CS159. Nathan Sprague. November 9, 2015

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Functions, Closures and Control Abstraction

4. Functional Programming Language-Oriented Programming

List are immutable Lists have recursive structure Lists are homogeneous

Recursive Definitions

Data Structures Lecture 3 Order Notation and Recursion

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Lecture 4: Higher Order Functions

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

We cover recursion in 150. Why do it again in 151?

F28PL1 Programming Languages. Lecture 14: Standard ML 4

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Note that pcall can be implemented using futures. That is, instead of. we can use

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

CSE 2123 Recursion. Jeremy Morris

CS Programming Languages: Scala

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)

CS558 Programming Languages

Homework 3 COSE212, Fall 2018

Announcements. CSCI 334: Principles of Programming Languages. Lecture 16: Intro to Scala. Announcements. Squeak demo. Instructor: Dan Barowy

CS1 Lecture 15 Feb. 19, 2018

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS/CE 2336 Computer Science II

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

CSCE 314 Programming Languages

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

CSC148-Section:L0301

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Outline. Collections Arrays vs. Lists Functions using Arrays/Lists Higher order functions using Arrays/Lists

Recursion. Example R1

CompSci 220. Programming Methodology 12: Functional Data Structures

CSC324 Principles of Programming Languages

Searching & Sorting in Java Bubble Sort

Programming Paradigms Written Exam (6 CPs)

Lab Manual. Program Design and File Structures (P): IT-219

Recursion & Iteration

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

CSC 533: Programming Languages. Spring 2015

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Building Java Programs

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Lecture 2. 1 Immutability. 2 Case Classes

Logic - CM0845 Introduction to Haskell

Functional programming with Common Lisp

CS 2340 Objects and Design - Scala

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Bibliography. Analyse et Conception Formelle. Lesson 5. Crash Course on Scala. Scala in a nutshell. Outline

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Lecture 8: Summary of Haskell course + Type Level Programming

Chapter 3 Linear Structures: Lists

Building Java Programs

Functional Programming in Haskell Part I : Basics

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

SCALA ARRAYS. Following picture represents array mylist. Here, mylist holds ten double values and the indices are from 0 to 9.

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

Trace call recsum(x, 3)

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community

Introduction to programming using Python

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Transcription:

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