Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
Computer Science 1620 Loops.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Hash Tables1 Part E Hash Tables  
Introducing the Erlang Language Why Erlang?  Exemplifies the “you can have code and concurrency that is free from side effects…there is no other way”
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
CS0007: Introduction to Computer Programming Introduction to Arrays.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
By the end of this session you should be able to...
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Built-in Data Structures in Python An Introduction.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Written by: Dr. JJ Shepherd
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
OOP Basics Classes & Methods (c) IDMS/SQL News
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
EGR 2261 Unit 11 Pointers and Dynamic Variables
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 22 – Searching
CSE 341 Lecture 20 Mutation; memoization slides created by Marty Stepp
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Coding Concepts (Basics)
Variables Title slide variables.
Lecture 14: The environment model (cont
(more) Python.
Dan Grossman / Eric Mullen Autumn 2017
Assignments and Procs w/Params
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
SPL – PS2 C++ Memory Handling.
Introduction to Computer Science
Presentation transcript:

Recap form last time How to do for loops map, filter, reduce Next up: dictionaries

Key data structure: Dictionaries Associative arrays, Hash tables … A table storing a set of “keys”, And a “value” for each key. Any (immutable) object can be a key! int,float,string,tuples… Very useful!

Using Dictionaries Unsorted list of key,value pairs Empty Dictionary: {} Non-empty Dictionary: {k1:v1,k2:v2,…} Membership: is k in dict: k in d Lookup value of key: d[k] Set value of key: d[k]=v

Dictionaries >>> d={} >>> d=dict(mexmenu) >>> d[“ceviche”] = 3.95 >>> d {…} >>> d[“burrito”] 3.50 >>> d.keys() … >>> d.values()

Dictionaries def freq(s): d={} for c in s: if c in d: d[c]+=1 else: d[c]=1 return d >>> d=plotfreq([1,1,3.0,”A”,3.0,”A”,”A”,1,2,3.0,1,”A”]) >>> d … >>> d = plotfreq(“avrakedavra”) >>> d.keys() >>> d … def plotfreq(s): d=freq(s) for k in d.keys(): print k, “*”*d[k]

Today Data model Sates how the programmer should think of data Answers the question: what do names refer to?

Data model in functional langs Environment of bindings (phonebook) Never change a binding –add new bindings at the end of the phonebook

Data model in functional langs Variables are names that refer into the phonebook Most recent entry looked up during evaluation Environment “frozen” inside function value so that the behavior of the function cannot be changed later on (easier reasoning)

Data model in OO langs Vars point to Objects Objects are boxes with data inside of them Can update the “link” that a name refers to, but can also update the “content” of the box that a name refers to. More on this later

Namespaces in Python Namespaces are used to manage variable names in Python In some ways similar to the concept of environments in ML However, different in some ways though: Python programmers can explicitly define new namespaces, and refer to them

Namespaces in Python Core PL concept that unifies many different ideas Not unique to Python of course, but in Python, namespaces are taken to the next level, as we shall soon see Remember our friend Morpheus: forget all you know about namespaces for now...

What’s a namespace Just a mapping from names to objects

What’s a namespace Wait, hold on a second!!! A mapping from names to objects? But, but, but... Isn’t that just a...

What’s a namespace Wait, hold on a second!!! A mapping from names to objects? But, but, but... Isn’t that just a... Dictionary in Python? Yes, but let’s defer that discussion to later...

Namespaces vs. Environments Battle of the language concepts!

Namespaces vs. Environments Battle of the language concepts! Let the race begin!

Seriously... Python Namespaces different from ML environments in two ways First difference: assignment! Basic operation in imperative langs: x = e In ML, let binding just added new binding In Python, assgn updates binding to point to new object

Simple example i,s = 0,0 while (i <= 3): i,s = i+1, s+i

Simple example i,s = 0,0 while (i <= 3): i,s = i+1, s+i Note: it’s the same name s, which points to different objects. The namespace is not extended, like an environment

Second difference Ok, so first difference is update: we can update what a name refers to Second difference we can also change what’s inside the box

Updates Well, for integers we can’t change what’s inside the box – these are immutable objects But for mutable objects, we can change what’s inside the box Examples of mutable objects:

Updates Well, for integers we can’t change what’s inside the box – these are immutable objects But for mutable objects, we can change what’s inside the box Examples of mutable objects: strings, lists, dictionaries, etc.

Updates: who cares? Ok, so we can change what’s inside the box. Big deal! Who cares? Well: Why is this new model conceptually different from saying “new box is built with the new updated value inside”?

Updates: who cares? Ok, so we can change what’s inside the box. Big deal! Who cares? Well: Why is this new model conceptually different from saying “new box is built with the new updated value inside”? Answer: aliasing!!! Go to code

Aliasing >>> x = [0] >>> y = x >>> y[0] = "a" >>> x

Aliasing When two or more names refer to the same object, we call this aliasing This never happens in pure functional languages –actually, that’s not really true: it does happen, but you just can’t tell because you can’t mutate any memory objects...

Aliasing Aliasing happens in almost any imperative language Java, Python: names map to objects (implicit pointers...) C: names “point” to memory cells (different from Java Python because the pointers are explicit)

Primitive types in Java So what about those primitives types in Java, like int, float, etc. For these types, var holds the value, not a pointer to value. Can you tell the difference?

Answer: no! You can’t tell the difference! You can explain all of Java by telling someone that x = y + z just creates a new immutable integer that x points to... The reason you can’t tell the different is that there are no explicit pointers.

Back to namespaces Recall: namespace is a map from names to objects Notion of namespace pervades Python You can even name a namespace, and then look it up Go to code!

Block namespace example y = 0 x = [10] def f(y): z = len(x) + y return z f(5)

Block namespace example y = 0 x = [10] def f(y): z = len(x) + y return z f(5) to get x: not in local, found in globals to get len: not in local, not in globals, found in builtin to get y: found in current (global binding shaded) write z in current when f is done, in this case throw the namespace away

Questions Why did I say “in this case”? Why is a stack not enough? When do we need to freeze a namespace and store it? Before answering this, let’s see some more examples (go back to code)

Moral of our code The moral: can change a function's behavior based on something we did AFTER the function definition. This could not happen in ML!!! This is very scary, because completely unrelated code can change the behavior of our function g.

Pattern So what's the pattern here? It's free variables! Be very careful with free vars... In fact, because of free vars are tricky, Python takes pains to ensure you don’t accidentally overwrite variables outside the current scope (go back to code)

What happened? How come we didn’t get “smash”? Python treats assignments differently from reads... When processing an assignment x = e, Python computes e, the looks up x in the current scope (namespace). If x is there, then it updates the binding If not, it introduces new binding for x def f(): n = "smash" print n f() n

What happened? This means that an assignment cannot change globals. Wow, that’s pretty amazing!!! Well, there is a workaround... Go back to code def f(): n = "smash" print n f() n

What happened? You may think it should print 11, and leave the global unchanged... but here is what really happens Since x is assigned in the function, it treats x as a local, and so it adds a binding for it. But then it looks up x for evaluating x+1, and it can’t find a local x  ERROR x = 10 def f(): x = x + 1 print x f()

What happened? Btw... Unlike in the previous case where we could add a binding for n afterwards, this function here is just hopeless... As an aside: do people believe these should be checked statically? x = 10 def f(): x = x + 1 print x f()

Recall our questions Why did I say “in this case”? Why is a stack not enough? When do we need to freeze a namespace and store it?

Well, recall ML It has to do with capturing a name inside a function definition Let’s see this! Back to code!

Next time Next time we’ll finally see classes