Reminder About Functions

Similar documents
Functions and Recursion

Functions and Recursion. Dr. Philip Cannata 1

fjyswan Dr. Philip Cannata 1

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

Programming Languages. Dr. Philip Cannata 1

Programming Languages. Dr. Philip Cannata 1

Higher-Order Functions

Programming Languages. Dr. Philip Cannata 1

Names and Types. standard hue names. Dr. Philip Cannata 1

Parsing Scheme (+ (* 2 3) 1) * 1

Higher-Order Functions (Part I)

Lexical Considerations

Decaf Language Reference

Lexical Considerations

1 Lexical Considerations

Extending Jython. with SIM, SPARQL and SQL

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CS2304: Python for Java Programmers. CS2304: Advanced Function Topics

Introduction to Programming (Java) 4/12

22c:111 Programming Language Concepts. Fall Functions

CSCE 110: Programming I

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

Functions & First Class Function Values

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

known as non-void functions/methods in C/C++/Java called from within an expression.

Introduction to Programming Using Java (98-388)

Principles of Programming Languages

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

DRAWING ENVIRONMENT DIAGRAMS

Writing a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G.

Module 10: Imperative Programming, Modularization, and The Future

Decaf Language Reference

CS 345. Functions. Vitaly Shmatikov. slide 1

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

Lecture 5: Methods CS2301

Syntax and Grammars 1 / 21

Object Model Comparisons

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Dynamic memory allocation

Writing Evaluators MIF08. Laure Gonnord

Type Declarations. [... <id> τ... ] <id> : τ. Γ <num> : number. Γ true : boolean. Γ false : boolean. Γ e 1 : number.

An Introduction to Functions

COMP520 - GoLite Type Checking Specification

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

Section 2.2 Your First Program in Java: Printing a Line of Text

Do not start the test until instructed to do so!

"Good" vs. "Bad" Expressions. ; interp-expr FAE... -> FAE-Value

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

Jython. secondary. memory

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

JAVA Programming Language Homework I - OO concept

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

CS111: PROGRAMMING LANGUAGE II

CS 6353 Compiler Construction Project Assignments

Scheme as implemented by Racket

Homework #3: CMPT-379

Charming Python with Static Typechecking

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Lab5. Wooseok Kim

CS558 Programming Languages

CSE P 501 Exam 8/5/04

Getting Started with Python

Programming Languages: Lecture 12

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

Chapter 6 Recursion. The Concept of Recursion

COMP 364: Functions II

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

Lecture #23: Conversion and Type Inference

An Overview of Compilation

Functional Programming - 2. Higher Order Functions

Chapter 1. Fundamentals of Higher Order Programming

PREPARING FOR PRELIM 1

Recap: Functions as first-class values

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Advanced Computer Programming

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1

Conversions and Overloading : Overloading

Advanced Python. Executive Summary, Session 1

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS115 - Module 9 - filter, map, and friends

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

IR Generation. May 13, Monday, May 13, 13

1. Download the JDK 6, from

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Program Representations

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CS558 Programming Languages

Transcription:

Reminder About Functions (let ((z 17)) (let ((z 3) (a ) (x (lambda (x y) (- x (+ y z))))) (let ((z 0) (a )) (x z a)))) int h, i; void B(int w) { int j, k; i = 2*w; w = w+1; void A(int x, int y) { bool i, j; B(h); parameters and local variables int main() { Return address int a, b; Saved registers h = ; a = 3; b = 2; Temporary variables A(a, b); Return value Dr. Philip Cannata 1

Calling function: factorial BasePtr = 3, printing runtime stack Calling function: factorial BasePtr =, printing runtime stack Calling function: factorial BasePtr = 7, printing runtime stack Calling function: factorial BasePtr = 9, printing runtime stack n: 0 Hmm Runtime Stack for Factorial 3 int factorial(int n) { if(n < 1) { return 1; else { return n * factorial(n - 1); int main() { int number, answer; number = 3; answer = factorial(number); print(answer); BasePtr = 9, printing runtime stack n: 0 BasePtr = 7, printing runtime stack BasePtr =, printing runtime stack return#factorial: 2 BasePtr = 3, printing runtime stack return#factorial: 2 return#factorial: 6 Dr. Philip Cannata 2

A Taxonomy of Functions 6.1 A Taxonomy of Functions first-order Functions are not values in the language. They can only be defined in a designated portion of the program, where they must be given names for use in the remainder of the program. The functions in F1WAE are of this nature, which explains the 1 in the name of the language. higher-order Functions can [be defined anywhere in a program and] return other functions as values. first-class Functions are values with all the rights of other values. In particular, they can be supplied as the value of arguments to functions, returned by functions as answers, and stored in data structures. [They can also be defined anywhere in a program.] Dr. Philip Cannata 3

Functions in Python/Jython http://www.penzilla.net/tutorials/python/functions/ Python and Functions: Python is a little different from other languages like C/C++ and Java, in that functions are not magic constructs, but values. Yes, functions in Python, are like integers in C. They are simply values and can be passed to other functions/object constructors, and so forth. We've already seen a pretty simple example of a Python function in the form of the venerable "Hello World!" program. Let's take another look at it: # hello.py def hello(): print "Hello World! return Dr. Philip Cannata 4

$ cat cs34.py x=3+4 def h (): print 'Hello' h h() print (lambda x, y : x + y)(2, 3) $ dist/bin/jython cs34.py Hello AST Functions in Python/Jython $ dist/bin/jython ast/astview.py cs34.py ('Module', ('body', ('Assign (1,0)', ('targets', ('Name (1,0)', ('id', 'x'), ('ctx', ('Store',)))), ('value', ('BinOp (1,2)', ('left', ('Num (1,2)', ('n', 3))), ('op', ('Add',)), ('right', ('Num (1,4)', ('n', 4)))))), ('FunctionDef (2,0)', ('name', 'h'), ('args', ('arguments', ('args',), ('vararg', None), ('kwarg', None), ('defaults',))), ('body', ('Print (3,3)', ('dest', None), ('values', ('Str (3,9)', ('s', 'Hello'))), ('nl', True))), ('decorator_list',)), ('Expr (4,0)', ('value', ('Name (4,0)', ('id', 'h'), ('ctx', ('Load',))))), ('Expr (,0)', ('value', ('Call (,0)', ('func', ('Name (,0)', ('id', 'h'), ('ctx', ('Load',)))), ('args',), ('keywords',), ('starargs', None), ('kwargs', None)))), ('Print (6,0)', ('dest', None), ('values', ('Call (6,6)', ('func', ('Lambda (6,7)', ('args', ('arguments', ('args', ('Name (6,)', ('id', 'x'), ('ctx', ('Param',))), ('Name (6,17)', ('id', 'y'), ('ctx', ('Param',)))), ('vararg', None), ('kwarg', None), ('defaults',))), ('body', ('BinOp (6,21)', ('left', ('Name (6,21)', ('id', 'x'), ('ctx', ('Load',)))), ('op', ('Add',)), ('right', ('Name (6,2)', ('id', 'y'), ('ctx', ('Load',)))))))), ('args', ('Num (6,28)', ('n', 2)), ('Num (6,31)', ('n', 3))), ('keywords',), ('starargs', None), ('kwargs', None))), ('nl', True)))) Dr. Philip Cannata

"let" Can Be Made Redundant In the textbook, the author says the following: Now that we have functions and function invocation as two distinct primitives, we can combine them to recover the behavior of "with" as a special case. Every time we encounter an expression of the form {with {var named body we can replace it with {{fun {var body named Example: In DrRacket: > (calc (parse '{with {x {+ {with {y {- x 3 {+ y y) ) In jython: >>> (lambda x : (lambda y : y + y) (x - 3) ) ( + ) Dr. Philip Cannata 6

"let" Can Be Made Redundant DrRacket (see Textbook, Chapter 3) v. Let in jython as in Homework #6 Welcome to DrRacket, version.0.2 [3m]. Language: plai; memory limit: 128 MB. > (calc (parse '{with {x {+ {+ x x) ) 20 > (calc (parse '{with {x {+ x x) ) > (calc (parse '{with {x {+ {with {y {- x 3 {+ y y) ) > (calc (parse '{with {x {+ x {with {x 3 x) ) 8 > (calc (parse '{with {x {+ x {with {x 3 ) ) 1 > (calc (parse '{with {x {+ x {with {y 3 x) ) > (calc (parse '{with {x {with {y x y) ) > (calc (parse '{with {x {with {x x x) ) $ dist/bin/jython Jython 2..2rc4 (trunk, Feb 27 2011, 21:3:17) >>> (let x: +: x+x)() 20 >>> (let x: : x+x)() >>> (let x: +:(let y: x-3: y+y)())() >>> (let x: : x + (let x: 3: x)())() 8 >>> (let x: : x + (let x: 3: )())() 1 >>> (let x: : x + (let y: 3: x)())() >>> (let x: : (let y: x: y)())() >>> (let x: : (let x: x: x)())() Dr. Philip Cannata 7