CSE : Python Programming
|
|
- Dwayne Owen
- 5 years ago
- Views:
Transcription
1 CSE : Python Programming Lecture 10: Functional programming, Memoization March 26,
2 Announcements Should have received about meeting times Length: ~15 minutes (try to show up on time!) Purpose: Give a high level overview of what you're doing Basic data structures and classes How do they interact What would you like my opinion on? Again, this is fairly informal Come prepared to talk and explain But no need to have anything like slides prepared 2
3 Functional programming
4 A quick survey Who has programmed in: Scheme Lisp Standard ML OCaml Haskell Some other functional language? 4
5 A quick survey Who has programmed in: Scheme Lisp What makes these languages "functional?" Standard ML OCaml Haskell Some other functional language? 4
6 A quick survey Who has programmed in: Scheme Lisp Standard ML OCaml What makes these languages "functional?" Better question: What's a dysfunctional language? Haskell Some other functional language? 4
7 What is functional programming? Depends on who you ask Two important features (in my mind): Minimal (or no!) use of mutating assignment Higher-order functions: Functions that take other functions as arguments Popular functional languages tend to also be strongly typed: types are checked at compile time and there are no implicit coercions 5
8 Why care about functional prog.? Standard answer: Reasoning about mutation is hard If we don't mutate anything, then everything acts as it would in pure mathematics A similar reason underlies why global state is bad Another answer: It's "more elegant" if functions are "firstclass", meaning that we can treat them like any other piece of data 6
9 Is Python a functional language? Depends on your coding style Mutating state seems to be a very common thing in Python programming: You want to modify structures Object-oriented programming, in practice, seems to also favor mutation Even if you think it's a functional language, "real" functional languages tend to be a bit more expressive 7
10 >>> q = [2, 3] >>> p = [1, q, 4] >>> print p [1, [2, 3], 4] >>> q[0] = 42 >>> print q [42, 3] >>> print p [1, [42, 3], 4] These first two assignments are fine, as far as functional programming is concerned. q 2 3 8
11 >>> q = [2, 3] >>> p = [1, q, 4] >>> print p [1, [2, 3], 4] >>> q[0] = 42 >>> print q [42, 3] >>> print p [1, [42, 3], 4] These first two assignments are fine, as far as functional programming is concerned. Makes me cringe to say that, because of iteration constructs q 2 3 8
12 >>> q = [2, 3] >>> p = [1, q, 4] >>> print p [1, [2, 3], 4] >>> q[0] = 42 >>> print q [42, 3] >>> print p [1, [42, 3], 4] p 1 4 q 2 3 9
13 >>> q = [2, 3] >>> p = [1, q, 4] >>> print p [1, [2, 3], 4] >>> q[0] = 42 >>> print q [42, 3] >>> print p [1, [42, 3], 4] p 1 4 q
14 >>> q = [2, 3] >>> p = [1, q, 4] >>> print p [1, [2, 3], 4] >>> q[0] = 42 >>> print q [42, 3] >>> print p [1, [42, 3], 4] This is the kind of assignment statement that functional programming avoids. The difference? Making a variable simply point to a (new) data structure versus modifying a data structure in place. 11
15 >>> q = [2, 3] >>> p = [1, q, 4] >>> print p [1, [2, 3], 4] >>> q[0] = 42 >>> print q [42, 3] >>> print p [1, [42, 3], 4] >>> p = [3, 4] This assignment is fine, as far as functional programming is concerned. The difference? Making a variable simply point to a (new) data structure versus modifying a data structure in place. 12
16 Nested functions >>> def foo(x, y):... def bar():... return y.append(x+2)... return bar... >>> zs = [2, 3] >>> f = foo(2, zs) >>> f() >>> zs [2, 3, 4] >>> zs.append(5) >>> zs [2, 3, 4, 5] >>> f() >>> zs [2, 3, 4, 5, 4] What happened here? bar doesn't define y, x x, y are not global So x, y are free in bar They must refer to the nearest enclosing scope, i.e., foo 13
17 Nested functions >>> def foo(x, y):... def bar():... return y.append(x+2)... return bar... >>> zs = [2, 3] >>> f = foo(2, zs) >>> f() >>> zs [2, 3, 4] >>> zs.append(5) >>> zs [2, 3, 4, 5] >>> f() >>> zs [2, 3, 4, 5, 4] When bar is returned from foo, it's packed up in an environment defining x, y Not a namespace, but the idea is similar When bar is executed, execute it in that environment 14
18 Nested functions >>> def foo(x, y):... def bar():... return y.append(x+2)... return bar... >>> zs = [2, 3] >>> f = foo(2, zs) >>> f() >>> zs [2, 3, 4] >>> zs.append(5) >>> zs [2, 3, 4, 5] >>> f() >>> zs [2, 3, 4, 5, 4] In the context of functional programming, it's important that we be able to construct new functions at run-time 15
19 Passing functions to other functions >>> def gt(x, y):... if x > y:... return elif x == y:... return 0... else:... return 1... >>> print sorted([10, 20, 4, 50]) [4, 10, 20, 50] >>> print sorted([10, 20, 4, 50], cmp = gt) [50, 20, 10, 4] 16
20 Passing functions to other functions >>> def snd(x):... return x[1]... >>> a = [(20, 5), (10, 10), (30, 40), (70, 2)] >>> print sorted(a) [(10, 10), (20, 5), (30, 40), (70, 2)] >>> print sorted(a, key = snd) [(70, 2), (20, 5), (10, 10), (30, 40)] 17
21 Lambda forms >>> a = [(20, 5), (10, 10), (30, 40), (70, 2)] >>> print sorted(a) [(10, 10), (20, 5), (30, 40), (70, 2)] >>> print sorted(a, key = lambda x: x[1]) [(70, 2), (20, 5), (10, 10), (30, 40)] 18
22 Lambda forms In Python, we can define nameless functions using the lambda keyword lambda arg1, arg2,... : expr The "expr" there is key: Only expressions are allowed, i.e., those constructs which evaluate to a value For example, no assignments or blocks Anonymous lambdas in "real" functional languages are, under the covers, how all functions are defined 19
23 Lambda forms lambda arg1, arg2,... : expr The "expr" there is key: Only expressions are allowed, i.e., those constructs which evaluate to a value Another point about functional programming Everything has a value! We don't evaluate something for its side-effects What's the value of an if statement or a while loop in Python? 20
24 Common higher-order functions map(f, a) filter(p, a) [ f(x) for x in a ] [ x for x in a if p(x) ] >>> reduce(lambda x, y : x ** y, [4, 3, 2, 1]) 4096 >>> reduce(lambda x, y : x + y, [4, 3, 2, 1]) 10 21
25 Common higher-order functions map(f, a) filter(p, a) [ f(x) for x in a ] [ x for x in a if p(x) ] >>> reduce(lambda x, y : x ** y, [4, 3, 2, 1]) 4096 >>> reduce(lambda x, y : x + y, [4, 3, 2, 1]) 10 "Real" functional languages call this "fold". 21
26 Partial applications >>> def f(x, y):... return x + y... >>> f(2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes exactly 2 arguments (1 given) Python doesn't let you partially apply functions, another common feature in "real" functional languages. 22
27 Perhaps I'm too hard on Python Just because there are a number of things which "real" functional languages "do better", that doesn't mean it's pointless to avoid functional programming in Python The benefits are all there in Python You just may not have as expressive constructs as you might like to make it feel entirely natural 23
28 Memoization
29 Recursion versus iteration def fib_iter(n) : a, b = 0, 1 for i in range(0, n - 1): a, b = b, a + b return a Fast, but a bit hard to make sense of. Very close to mathematical definition, but very slow. def fib_rec(n) : if n <= 1: return n else: return fib(n - 1) + fib(n - 2) 25
30 Recursion versus iteration def fib_iter(n) : a, b = 0, 1 for i in range(0, n - 1): a, b = b, a + b return a Functional programming prefers recursion. (What's that i in the for loop doing?) def fib_rec(n) : if n <= 1: return n else: return fib(n - 1) + fib(n - 2) 25
31 fib(5) fib(4) fib(3) fib(3) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(2) fib(1) fib(0) 26
32 fib(5) fib(4) fib(3) fib(3) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(2) fib(1) fib(0) 26
33 fib(5) fib(4) fib(3) fib(3) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(2) fib(1) fib(0) 26
34 Memoized version of fib def fib_mem(n, ans = {}) : if n in ans: return ans[n] if n <= 1: ans[n] = n else: ans[n] = fib_mem(n - 1) + fib_mem(n - 2) return ans[n] 27
35 Memoized version of fib def fib_mem(n, ans = {}) : if n in ans: return ans[n] if n <= 1: ans[n] = n else: ans[n] = fib_mem(n - 1) + fib_mem(n - 2) return ans[n] We use ans here to keep track of the results of previous calls to fib_mem. Users should never supply a value for this argument. 27
36 Memoized version of fib def fib_mem(n, ans = {}) : if n in ans: return ans[n] if n <= 1: ans[n] = n else: ans[n] = fib_mem(n - 1) + fib_mem(n - 2) return ans[n] Recall that the value of this default argument is initialized once, when the function is defined. All the modifications in the function update the value. 28
37 Memoized version of fib def fib_mem(n, ans = {}) : if n in ans: return ans[n] if n <= 1: ans[n] = n else: ans[n] = fib_mem(n - 1) + fib_mem(n - 2) return ans[n] Downside: We've traded off memory usage for speed. (Also: is this in the spirit of functional programming?) 29
CSE : Python Programming
CSE 399-004: Python Programming Lecture 2: Data, Classes, and Modules January 22, 2007 http://www.seas.upenn.edu/~cse39904/ Administrative things Teaching assistant Brian Summa (bsumma @ seas.upenn.edu)
More informationCSE : Python Programming
CSE 399-004: Python Programming Lecture 08: Graphical User Interfaces with wxpython March 12, 2005 http://www.seas.upenn.edu/~cse39904/ Plan for today and next time Today: wxpython (part 1) Aside: Arguments
More informationSets. identity maps, unordered collection
Sets identity maps, unordered collection Sets [] for lists, () for tuples, {} for dicts, and {} for sets (2.7) construction from lists, tuples, dicts (keys), and strs in, not in, add, remove = {1, 2} a
More informationPython for C programmers
Python for C programmers The basics of Python are fairly simple to learn, if you already know how another structured language (like C) works. So we will walk through these basics here. This is only intended
More informationIntroduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017
Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Spring 2017 Outline Functions State Functions as building blocks Higher order functions Map Reduce
More informationCIS192: Python Programming
CIS192: Python Programming Functions and Functional Programming Harry Smith University of Pennsylvania January 25, 2018 Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 1 / 39
More informationIntro. Speed V Growth
Intro Good code is two things. It's elegant, and it's fast. In other words, we got a need for speed. We want to find out what's fast, what's slow, and what we can optimize. First, we'll take a tour of
More informationPython Programming: Lecture 3 Functions
Python Programming: Lecture 3 Functions Lili Dworkin University of Pennsylvania Last Week s Quiz In one line, write code that will turn the list [9, 9, 9] into the string 90909. Last Week s Quiz In one
More informationCIS192 Python Programming
CIS192 Python Programming Functional Programming Eric Kutschera University of Pennsylvania January 30, 2015 Eric Kutschera (University of Pennsylvania) CIS 192 January 30, 2015 1 / 31 Questions Homework
More informationTAIL RECURSION, SCOPE, AND PROJECT 4 11
TAIL RECURSION, SCOPE, AND PROJECT 4 11 COMPUTER SCIENCE 61A Noveber 12, 2012 1 Tail Recursion Today we will look at Tail Recursion and Tail Call Optimizations in Scheme, and how they relate to iteration
More information61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS
61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS Steven Tang and Eric Tzeng July 18, 2013 Now in a wider screen format! Who am I? What am I doing here? First two weeks of class (Chapter 1): FUNCTIONS Computational
More informationINF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.
INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION Instructors: Crista Lopes Copyright Instructors. Topics Recursion Higher-order functions Continuation-Passing Style Monads (take 1) Identity Monad Maybe
More informationCIS192 Python Programming
CIS192 Python Programming Functional Programming Robert Rand University of Pennsylvania February 03, 2016 Robert Rand (University of Pennsylvania) CIS 192 February 03, 2016 1 / 23 Outline 1 Function Arguments
More informationCSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion
CSC 148 Lecture 3 Dynamic Typing, Scoping, and Namespaces Recursion Announcements Python Ramp Up Session Monday June 1st, 1 5pm. BA3195 This will be a more detailed introduction to the Python language
More informationSD314 Outils pour le Big Data
Institut Supérieur de l Aéronautique et de l Espace SD314 Outils pour le Big Data Functional programming in Python Christophe Garion DISC ISAE Christophe Garion SD314 Outils pour le Big Data 1/ 35 License
More informationFunctional Languages. Hwansoo Han
Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm
More informationSCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017
SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationDesign and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization
Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Module 02 Lecture - 45 Memoization Let us continue our discussion of inductive definitions. (Refer Slide Time: 00:05)
More informationINF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.
INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION Instructors: James Jones Copyright Instructors. Topics Recursion Higher-order functions Continuation-Passing Style Monads (take 1) Identity Monad
More informationIntro. Scheme Basics. scm> 5 5. scm>
Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if
More informationProgramming Language Pragmatics
Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken Alan Turing, Alonzo Church, Stephen
More informationFunctions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)
CHAPTER 5 Functions The main ideas in this chapter are: first-class functions: functions are values that can be passed as arguments to other functions, returned from functions, stored in lists and dictionaries,
More informationMeasuring Efficiency
Growth Announcements Measuring Efficiency Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: fib(3) fib(5) fib(4) def fib(n): if n == 0: return 0 elif n == 1: return 1
More informationCSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern
CSE 399-004: Python Programming Lecture 12: Decorators April 9, 200 http://www.seas.upenn.edu/~cse39904/ Announcements Projects (code and documentation) are due: April 20, 200 at pm There will be informal
More informationCS 11 Haskell track: lecture 1
CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of
More informationLECTURE 16. Functional Programming
LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative
More informationSummer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define
CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee 1 Contents 1. Historical Background of Haskell 2. Lazy, Pure, and Functional Language 3. Using ghc and ghci 4. Functions 5. Haskell Scripts
More information4. Functional Programming Language-Oriented Programming
4. Functional Programming Language-Oriented Programming Prof. Dr. Bernhard Humm Faculty of Computer Science Hochschule Darmstadt University of Applied Sciences 1 Retrospect: LOP the big picture What is
More informationConcepts of Programming Languages
Concepts of Programming Languages Lecture 15 - Functional Programming Patrick Donnelly Montana State University Spring 2014 Patrick Donnelly (Montana State University) Concepts of Programming Languages
More informationpeval Documentation Release Bogdan Opanchuk
peval Documentation Release 0.1.0 Bogdan Opanchuk January 29, 2016 Contents 1 Introduction 1 2 Implementation details 3 3 Restrictions on functions 5 4 API reference 7 4.1 Core functions..............................................
More informationChapter 11 :: Functional Languages
Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Copyright 2016 Elsevier 1 Chapter11_Functional_Languages_4e - Tue November 21, 2017 Historical Origins The imperative
More informationFunctional Languages. CSE 307 Principles of Programming Languages Stony Brook University
Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Basics 2 Contents 1. Jump into Haskell: Using ghc and ghci (more detail) 2. Historical Background of Haskell 3. Lazy, Pure, and Functional
More informationSCHEME AND CALCULATOR 5b
SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More information6.001 Notes: Section 8.1
6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything
More informationFunctional Programming
Functional Programming Björn B. Brandenburg The University of North Carolina at Chapel Hill Based in part on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts. Brief
More informationCS1 Lecture 5 Jan. 25, 2019
CS1 Lecture 5 Jan. 25, 2019 HW1 due Monday, 9:00am. Notes: Do not write all the code at once before starting to test. Take tiny steps. Write a few lines test... add a line or two test... add another line
More information1 Decorators. 2 Descriptors. 3 Static Variables. 4 Anonymous Classes. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers July 13, / 19
1 Decorators 2 Descriptors 3 Static Variables 4 Anonymous Classes Sandeep Sadanandan (TU, Munich) Python For Fine Programmers July 13, 2009 1 / 19 Decorator Pattern In object-oriented programming, the
More informationIntroduction to Python Code Quality
Introduction to Python Code Quality Clarity and readability are important (easter egg: type import this at the Python prompt), as well as extensibility, meaning code that can be easily enhanced and extended.
More informationCSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures
CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught
More information# track total function calls using a global variable global fibcallcounter fibcallcounter +=1
Math 4242 Fibonacci, Memoization lecture 13 Today we talk about a problem that sometimes occurs when using recursion, and a common way to fix it. Recall the Fibonacci sequence (F 0, F 1, F 2,...) that
More informationCSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN
What languages do you know? CSC 326H1F, Programming Languages The usual suspects: C, C++, Java fine languages nearly the same Perhaps you've also learned some others? assembler Basic, Visual Basic, Turing,
More informationCOP4020 Programming Languages. Functional Programming Prof. Robert van Engelen
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts
More informationQuick announcement. Midterm date is Wednesday Oct 24, 11-12pm.
Quick announcement Midterm date is Wednesday Oct 24, 11-12pm. The lambda calculus = ID (λ ID. ) ( ) The lambda calculus (Racket) = ID (lambda (ID) ) ( )
More informationCSE 341 Lecture 27. JavaScript scope and closures. slides created by Marty Stepp
CSE 341 Lecture 27 JavaScript scope and closures slides created by Marty Stepp http://www.cs.washington.edu/341/ Recall: Scope scope: The enclosing context where values and expressions are associated.
More informationScheme: Expressions & Procedures
Scheme: Expressions & Procedures CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, March 31, 2017 Glenn G. Chappell Department of Computer Science University
More informationCS 360 Programming Languages Interpreters
CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like
More informationCS457/557 Functional Languages
CS457/557 Functional Languages Spring 2018 Lecture 1: Course Introduction Andrew Tolmach Portland State University (with thanks to Mark P. Jones) 1 Goals of this course Introduce the beautiful ideas of
More informationProgramming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python
Programming Languages Streams Wrapup, Memoization, Type Systems, and Some Monty Python Quick Review of Constructing Streams Usually two ways to construct a stream. Method 1: Use a function that takes a(n)
More informationCSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria
CSE 399-004: Python Programming Lecture 5: Course project and Exceptions February 12, 2007 Announcements Still working on grading Homeworks 3 and 4 (and 2 ) Homework 5 will be out by tomorrow morning I
More informationRuby: Introduction, Basics
Ruby: Introduction, Basics Computer Science and Engineering College of Engineering The Ohio State University Lecture 4 Ruby vs Java: Similarities Imperative and object-oriented Classes and instances (ie
More informationCS1 Lecture 5 Jan. 26, 2018
CS1 Lecture 5 Jan. 26, 2018 HW1 due Monday, 9:00am. Notes: Do not write all the code at once (for Q1 and 2) before starting to test. Take tiny steps. Write a few lines test... add a line or two test...
More informationWeek 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion
Week 2: The Clojure Language Background Basic structure A few of the most useful facilities A modernized Lisp Review of Lisp's origins and development Why did Lisp need to be modernized? Relationship to
More informationLecture 5: Lazy Evaluation and Infinite Data Structures
Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a
More informationCSE 341, Autumn 2015, Ruby Introduction Summary
CSE 341, Autumn 2015, Ruby Introduction Summary Disclaimer: This lecture summary is not necessarily a complete substitute for atting class, reading the associated code, etc. It is designed to be a useful
More informationLogo Features We'll first present a sample Logo program, and then discuss each component separately.
CS61A Notes Week 12 The Logo Programming Language [Solutions] The Logo Programming Language The Logo programming language was developed in the late 1960 s as a tool for teaching programming. Its simple
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions
More informationFunctions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)
CHAPTER 5 Functions The main ideas in this chapter are: first-class functions: functions are values that can be passed as arguments to other functions, returned from functions, stored in lists and dictionaries,
More informationAdvanced topics, part 2
CS 1 Introduction to Computer Programming Lecture 24: December 5, 2012 Advanced topics, part 2 Last time Advanced topics, lecture 1 recursion first-class functions lambda expressions higher-order functions
More informationCS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.
CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3
More informationCS116 - Module 5 - Accumulative Recursion
CS116 - Module 5 - Accumulative Recursion Cameron Morland Winter 2018 1 Cameron Morland CS116 - Module 5 - Accumulative Recursion Types of Recursion Structural Recursion Generative Recursion Accumulative
More informationCSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Spring 2016 Type-checking (Static) type-checking can reject a program before it runs to prevent the possibility of some errors A feature
More informationCSE 341: Programming Languages
CSE 341: Programming Languages Winter 2005 Lecture 17 varargs and apply, implementing higher-order functions CSE341 Winter 2005, Lecture 17 1 Today: Some easy Scheme odds and ends Implementing higher-order
More informationCSC324 Functional Programming Efficiency Issues, Parameter Lists
CSC324 Functional Programming Efficiency Issues, Parameter Lists Afsaneh Fazly 1 January 28, 2013 1 Thanks to A.Tafliovich, P.Ragde, S.McIlraith, E.Joanis, S.Stevenson, G.Penn, D.Horton 1 Example: efficiency
More informationCSCC24 Functional Programming Scheme Part 2
CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages
More informationHIGHER ORDER FUNCTIONS 2
HIGHER ORDER FUNCTIONS 2 COMPUTER SCIENCE 61A June 21, 2012 1. You already saw in lecture a method to check if a number is prime: def is_prime(n): k = 2 while k < n: if n % k == 0: return False k += 1
More informationProgramming Languages
CSE 130 : Spring 2011 Programming Languages Lecture 13: What s in a Name? Ranjit Jhala UC San Diego Next: What s in a name? More precisely: How should programmer think of data What does a variable x really
More informationFunctions parameters, scope CS GMU
Functions parameters, scope CS 112 @ GMU Functions function: we can name a batch of statements, to call later. a function can accept some expected number of values to be supplied each time it is called
More informationEnvironments
Environments PLAI Chapter 6 Evaluating using substitutions is very inefficient To work around this, we want to use a cache of substitutions. We begin evaluating with no cached substitutions, then collect
More informationNext: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?
Next: What s in a name? CSE 13 : Fall 211 Programming Languages Lecture 13: What s in a Name? More precisely: How should programmer think of data What does a variable x really mean? Ranjit Jhala UC San
More informationCS1 Lecture 15 Feb. 18, 2019
CS1 Lecture 15 Feb. 18, 2019 HW4 due Wed. 2/20, 5pm Q2 and Q3: it is fine to use a loop as long as the function is also recursive. Exam 1, Thursday evening, 2/21, 6:30-8:00pm, W290 CB You must bring ID
More informationFunctional programming in LISP
Programming Languages Week 4 Functional programming in LISP College of Information Science and Engineering Ritsumeikan University review of part 3 enumeration of dictionaries you receive a sequence of
More informationCSE 505: Concepts of Programming Languages
CSE 505: Concepts of Programming Languages Dan Grossman Fall 2003 Lecture 6 Lambda Calculus Dan Grossman CSE505 Fall 2003, Lecture 6 1 Where we are Done: Modeling mutation and local control-flow Proving
More information6.001 Notes: Section 6.1
6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of
More informationCS 11 python track: lecture 4
CS 11 python track: lecture 4 Today: More odds and ends assertions "print >>" syntax more on argument lists functional programming tools list comprehensions More on exception handling More on object-oriented
More informationRacket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.
Racket Next 2+ weeks will use the Racket language (not ML) and the DrRacket programming environment (not emacs) Installation / basic usage instructions on course website CSE34: Programming Languages Lecture
More informationCSC148 winter stools, names, tracing week 5
1/18 CSC148 winter 2014 stools, names, tracing week 5 Danny Heap / Dustin Wehr heap@cs.toronto.edu / dustin.wehr@utoronto.ca BA4270 / SF4306D http://www.cdf.toronto.edu/~heap/148/f13/ February 5, 2014
More information61A Lecture 2. Friday, August 28, 2015
61A Lecture 2 Friday, August 28, 2015 Names, Assignment, and User-Defined Functions (Demo) Types of Expressions Primitive expressions: 2 add 'hello' Number or Numeral Name String Call expressions: max
More informationOrganization of Programming Languages CS3200/5200N. Lecture 11
Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages
More informationRuby: Introduction, Basics
Ruby: Introduction, Basics Computer Science and Engineering College of Engineering The Ohio State University Lecture 3 Ruby vs Java: Similarities Imperative and object-oriented Classes and instances (ie
More informationLecture 16: Static Semantics Overview 1
Lecture 16: Static Semantics Overview 1 Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis
More informationStop coding Pascal. Saturday, April 6, 13
Stop coding Pascal...emotional sketch about past, present and future of programming languages, Python, compilers, developers, Life, Universe and Everything Alexey Kachayev CTO at KitApps Inc. Open source
More informationCOSE212: Programming Languages. Lecture 3 Functional Programming in OCaml
COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of
More informationAnnouncements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.
The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper
More informationCSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018
CSE341: Programming Languages Lecture 15 Macros Zach Tatlock Winter 2018 What is a macro A macro definition describes how to transform some new syntax into different syntax in the source language A macro
More informationWhat is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018
What is a macro A macro definition describes how to transform some new syntax into different syntax in the source language CSE341: Programming Languages Lecture 15 Macros Zach Tatlock Winter 2018 A macro
More informationProgramming Paradigms
Programming Paradigms Procedural Sequence of instructions that inform the computer what to do with the program's input Declarative Specification describes the problem to be solved, and language implementation
More informationLecture 8. Conditionals & Control Flow
Lecture 8 Conditionals & Control Flow Announcements For This Lecture Readings Sections 5.1-5.7 today Chapter 4 for Tuesday Assignment 2 Posted Today Written assignment Do while revising A1 Assignment 1
More informationCSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013
CSE341: Programming Languages Lecture 9 Function-Closure Idioms Dan Grossman Winter 2013 More idioms We know the rule for lexical scope and function closures Now what is it good for A partial but wide-ranging
More informationFunctional Programming. Big Picture. Design of Programming Languages
Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics
More informationOnion Language Reference Manual. By: Andrew Aday, (aza2112) Amol Kapoor (ajk2227), Jonathan Zhang (jz2814)
Onion Language Reference Manual By: Andrew Aday, (aza2112) Amol Kapoor (ajk2227), Jonathan Zhang (jz2814) Introduction 3 Types 3 Lexical Conventions 4 Identifiers 4 Keywords 4 Comments 4 Expressions 5
More informationLecture 13 CIS 341: COMPILERS
Lecture 13 CIS 341: COMPILERS Announcements HW4: OAT v. 1.0 Parsing & basic code generation Due: March 28 th START EARLY! Midterm Exam Grades Available on Gradescope Zdancewic CIS 341: Compilers 2 Midterm
More informationCS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise
CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise If you re not already crazy about Scheme (and I m sure you are), then here s something to get
More informationCSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009
CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009 Lecture notes for CS 6110 (Spring 09) taught by Andrew Myers at Cornell; edited by Amal Ahmed, Fall 09. 1 Static vs. dynamic scoping The scope of a variable
More informationTo figure this out we need a more precise understanding of how ML works
Announcements: What are the following numbers: 74/2/70/17 (2:30,2:30,3:35,7:30) PS2 due Thursday 9/20 11:59PM Guest lecture on Tuesday 9/25 o No RDZ office hours next Friday, I am on travel A brief comment
More informationCSE 341: Programming Languages
CSE 341: Programming Languages Autumn 2005 Lecture 10 Mutual Recursion, Equivalence, and Syntactic Sugar CSE 341 Autumn 2005, Lecture 10 1 Mutual Recursion You ve already seen how multiple functions can
More informationHigher Order Functions
Higher Order Functions Recall that in Python, functions that we define are just objects like anything else: In [1]: def meaningoflife(x) : return 42 * x type(meaningoflife) Out[1]: function This means
More informationPython. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline
Python Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar December 28, 2011 1 Outline Introduction Installation and Use Distinct Features Python Basics Functional Example Comparisons with
More informationStreams and Evalutation Strategies
Data and Program Structure Streams and Evalutation Strategies Lecture V Ahmed Rezine Linköpings Universitet TDDA69, VT 2014 Lecture 2: Class descriptions - message passing ( define ( make-account balance
More information