Module 10: Imperative Programming, Modularization, and The Future

Similar documents
CS115 - Module 10 - General Trees

Module 8: Local and functional abstraction

CS115 - Module 9 - filter, map, and friends

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

CS2500 Exam 2 Fall 2011

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Local defini1ons. Func1on mul1ples- of

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

A Brief Introduction to Scheme (I)

Module 5: Lists. Readings: HtDP, Sections 9, 10.

CS115 - Module 4 - Compound data: structures

Module 10: General trees

CS115 - Module 3 - Booleans, Conditionals, and Symbols

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Extensible Pattern Matching

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

Module 3: New types of data

CS116 - Module 5 - Accumulative Recursion

CS115 - Module 8 - Binary trees

List Comprehensions and Simulations

CSCC24 Functional Programming Scheme Part 2

Assignment: 7. Due: Language level: Allowed recursion:

Module 05: Types of recursion

Intro. Scheme Basics. scm> 5 5. scm>

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Functional Programming Languages (FPL)

Functional Programming and Haskell

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

How to Design Programs

Functional Programming in Haskell for A level teachers

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

Problem Set 4: Streams and Lazy Evaluation

Outline. Helper Functions and Reuse. Conditionals. Evaluation Rules for cond. Design Recipe with cond. Compound Data

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Module 01: Introduction to Programming in Python

Module 08: Searching and Sorting Algorithms

CS 115 Lecture Notes Winter 2019

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

Welcome to CS 115 (Winter 2018)

A. Incorrect! This would be the negative of the range. B. Correct! The range is the maximum data value minus the minimum data value.

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Python lab session 1

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Welcome to CS 115 (Winter 2019)

Functions & First Class Function Values

Mutable References. Chapter 1

Scheme as implemented by Racket

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Repetition Through Recursion

A CRASH COURSE IN SEMANTICS

Lecture 5: Lazy Evaluation and Infinite Data Structures

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

CSCE 314 Programming Languages

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Shell CSCE 314 TAMU. Higher Order Functions

Organization of Programming Languages CS3200/5200N. Lecture 11

Welcome to CS 135 (Winter 2018)

CircuitPython 101: Working with Lists, Iterators and Generators

CS 11 Haskell track: lecture 1

6.001 Notes: Section 15.1

15 212: Principles of Programming. Some Notes on Induction

Downloaded from Chapter 2. Functions

Chapter 15. Functional Programming Languages

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Introduction to Functional Programming in Haskell 1 / 56

6.001 Notes: Section 4.1

This session. Recursion. Planning the development. Software development. COM1022 Functional Programming and Reasoning

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Unit 7. 'while' Loops

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

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

Recursion CS GMU

RECURSION, RECURSION, (TREE) RECURSION! 3

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Programming Languages and Techniques (CIS120)

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

Class Structure. Prerequisites

Trombone players produce different pitches partly by varying the length of a tube.

Copied from: on 3/20/2017

Transcription:

Module 10: Imperative Programming, Modularization, and The Future If you have not already, make sure you Read How to Design Programs Sections 18. 1 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Functional Programming Throughout this course we have been using the functional programming paradigm. Function programming: treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. ( define ( factorial n) (cond [(= n 0) 1] [else (* n (factorial (- n 1)))])) Our function returns either a constant or the result returned by some function. By now this may be so natural to not need to be mentioned. But this is not the only way to approach program design! 2 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Imperative Programming An alternative way of thinking about program design is the imperative programming paradigm. Imperative programming: uses statements that change a program s state, [and] consists of commands for the computer to perform. def factorial(n): product = 1 while n > 0: product = product * n n = n - 1 return product In this Python program, I instruct the computer to store a value in a new variable product. I then instruct the computer to repeately change these values, while some condition is true. The function returns a value determined by following a series of instructions. 3 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Language does not dictate paradigm This course has been in Racket; it has followed the functional paradigm. CS 116 is in Python; sections of it are in the functional paradigm, but other sections are in the imperative paradigm. The choice of language does not dictate the paradigm. Some languages may make one paradigm easier to follow than the other. Racket supports functional programming very well, but has a full suit of instructions that support imperative programming: loops using for and while, variable re-assignment using set!, etc. These are not part of this course. Python supports imperative programming very well, but has many tools that support functional programming: map, filter, lambda, functools.reduce, and recursion. Different programming paradigms are better in different contexts. Programmers who are familiar with multiple paradigms will be better programmers. 4 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Moving toward imperative programming: local Consider a simple function that calculates the average of a list of numbers: ;; ( mean X) return the mean of X. ;; mean: (listof Num) -> Num (define (mean X) (/ (foldr + 0 X) (length X))) If I have a list I can operate on it and store the result in a constant: (define my-list (list 4 6 20)) (define my-mean (mean my-list)) But so far, we cannot create a new constant within a function. We cannot yet make constants that depends on the parameters our functions consume. 5 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Example: Standard Deviation Standard deviation is a measure widely used in statistics. The standard deviation σ of a set of N values {x 1, x 2,..., x N }, where µ is the mean of the values in X, is given by σ = 1 N (x i µ) N 2 i=1 I can write a function to calculate this, using mean: ;; ( std X) return the standard deviation of X. ;; std: (listof Num) -> Num (define (std X) (sqrt (/ (foldr + 0 (map (lambda (x) (sqr (- x (mean X)))) X)) (length X)))) 6 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Example: Standard Deviation ;; ( std X) return the standard deviation of X. ;; std: (listof Num) -> Num (define (std X) (sqrt (/ (foldr + 0 (map (lambda (x) (sqr (- x (mean X)))) X)) (length X)))) This function will work properly. But there is still a problem with it: the computer needs to re-compute (mean X) many times, once for each item in X. In this course we do not discuss algorithmic efficiency, but you can imagine that re-calculating something is wasteful. Wouldn t it be great if we could compute the mean once, and store the result in a constant? 7 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Storing partial work with local Using the local keyword we can compute one or more values and store the results in constants. The value of each constant can depend on the the parameters of the function, or on previously defined local constants. local has two parameters: 1 In square brackets [...], zero or more definitions. 2 An expression that may use these definitions. local evaluates to the value of this expression. ( define ( my-fun n) (local [; local definitions (define local-var...) ]... ; an expression )) 8 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Storing partial work with local ;; ( sum-evens-minus-sum-odds L) return the sum of the even values in L ;; minus the sum of the odd values. ;; sum-evens-minus-sum-odds: ( listof Int) -> Int ( define ( sum-evens-minus-sum-odds L) (local [ ( define odds ( filter odd? L)) ; store odd numbers ( define evens ( filter even? L)) ; store even numbers ( define sum-odds ( foldr + 0 odds)) ; store sum of odds ( define sum-evens ( foldr + 0 evens)) ; store sum of evens ] (- sum-evens sum-odds) ; expression of final answer. )) This code does very little work in the final expression. Almost all the work is done in defining the constants. 9 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Storing partial work with local It is not necessary to do any computation in the final expression; it could simply be a constant! ;; ( sum-evens-minus-sum-odds L) return the sum of the even values in L ;; minus the sum of the odd values. ;; sum-evens-minus-sum-odds: ( listof Int) -> Int ( define ( sum-evens-minus-sum-odds L) (local [ ( define odds ( filter odd? L)) ; store odd numbers ( define evens ( filter even? L)) ; store even numbers ( define sum-odds ( foldr + 0 odds)) ; store sum of odds ( define sum-evens ( foldr + 0 evens)) ; store sum of evens ( define final-answer (- sum-evens sum-odds)) ] final-answer ; just return a constant! )) Exercise Write a function (normalize L) that consumes a (listof Num), and returns the list containing each value in L divided by the sum of the values in L. Compute the sum only once. (normalize (list 4 2 14)) => (list 0.2 0.1 0.7)

Example: Standard Deviation This lets us rewrite std so it computes the mean of X just once, storing it in the constant mu. ;; ( std X) return the standard deviation of X. ;; std: (listof Num) -> Num (define (std X) (local [ ( define mu ( mean X)) ; a constant that stores the mean of X. ] (sqrt (/ (foldr + 0 (map (lambda (x) (sqr (- x mu))) X)) (length X))))) 11 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Example: Standard Deviation...Or rewrite the function so it does things step-by-step, storing values as it goes along: ;; ( std X) return the standard deviation of X. ;; std: (listof Num) -> Num (define (std X) (local [ (define mu (mean X)) ( define differences ( map ( lambda (x) (- x mu)) X)) ( define sqr-differences ( map sqr differences)) ( define sum-sqr-differences ( foldr + 0 sqr-differences)) (define N (length X)) ( define sum-over-n (/ sum-sqr-differences N)) ( define final-answer ( sqrt sum-over-n)) ] final-answer )) 12 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Practice with local Exercise Write a function (sum-odds-or-evens L) that consumes a (listof Int). If there are more evens than odds, the function returns the sum of the evens. Otherwise, it returns the sum of the odds. Use local, but do not use L more than twice (in map, filter, foldr, or otherwise). (sum-odds-or-evens (list 1 3 5 20 30)) => 9 13 CS 115 Module 10: Imperative Programming, Modularization, and The Future

local functions We are not limited to only defining constants inside local. We can also use it to define local functions. This is very similar to lambda, but allows us to name our functions. ;; ( keep-multiples n L) return all values in L which are divisible by n. ;; keep-multiples : Nat ( listof Nat) -> ( listof Nat) ;; Examples : ( check-expect ( keep-multiples 7 ( list 2 3 5 28 7 3 14 77)) ( list 28 7 14 77)) ( define ( keep-multiples n L) (local [ ;; ( divisible-n? x) return #true if x is divisible by n, else #false. ;; divisible-n?: Nat -> Bool ( define ( divisible-n? x) (= 0 (remainder x n))) ] ( filter divisible-n? L)))! Complete the design recipe for all local functions. Omit tests and examples.

local functions Exercise Create a function (even-mean-minus-odd-mean L) that returns the mean of the even values in L minus the mean of the odd values. Include a local helper function (mean M) that consumes a (listof Int) and returns the mean of the values in M. Do not create any additional helper functions. (even-mean-minus-odd-mean (list 16 14 5 1)) => 12 15 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Encapsulation and Modularity Another use of local is encapsulation. It allows us to create a helper function that we can use inside our primary function, but which is not available outside the primary function. For example, suppose we had created a mean function as before, but in some context we wanted another function called mean, that did something else. We can simultaneously have global function and a local function with the same name. ;; ( mean L) return the mean of the values in L (define (mean L) (/ (foldr + 0 L) (length L))) ;; ( rms-fun L) do something with the rms-mean of L ( define ( rms-fun L) ( local [;; ( mean L) return the rms-mean of the values in L ;; mean: (listof Num) -> Num ( define ( mean L) ; when I say mean, I mean root-mean-squared! (sqrt (foldr + 0 (map sqr L)))) ] (* 20 (mean L))))

Summary: uses for local There are several ways that local can be useful. Using it we can avoid re-computing the same value, making our code more efficient; use a named function instead of a lambda, potentially making our code easier to read; name a part of a computation, again making our code easier to read; let us encapsulate and modularize our code. We can make helpers that are not expected to be used outside a specific function. These changes start us shifting away from a pure functional programming paradigm. You will become more fluent at imperative programming when you take CS116. 17 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Module summary Be able to use local to define local constants, based on the values of parameters of your function. Be able to use local to define local functions, which are usable only inside the main function. Start to think imperatively: consider functions as a series of steps that the computer performs, one after the other. 18 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Wrapping up CS115 I want to highlight: The importance of communication, to other programmers but even to yourself. That data definitions can guide the design of our programs. These big ideas transcend language. They will influence your work in CS116 and beyond. 19 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Looking ahead to CS116 CS116 uses Python. The syntax of this language is less restricted, which means There are more ways to say things (which makes things easier to say!) There are more ways to say things (there is more syntax to learn) We will work on Writing programs that are longer, or deal with large amounts of data Designing programs so they run efficiently 20 CS 115 Module 10: Imperative Programming, Modularization, and The Future

Options for continuing CS CS major: Take CS 116 and then CS 136. Talk to a CS advisor about the process. Many students have been successful in CS after starting in CS 115. Computing Technology Option: Take CS 116. Talk to an advisor to understand your choices. Note: Participate in course selection! Otherwise, you may not be able to enroll in your preferred courses. 21 CS 115 Module 10: Imperative Programming, Modularization, and The Future