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

Similar documents
CS 314 Principles of Programming Languages

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Imperative languages

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Programming Languages

Functional Programming and Haskell

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Functional Programming

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

Functional Programming. Pure Functional Languages

Functional Programming

Functional Programming in C++

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

A Brief Introduction to Scheme (I)

Refactoring to Functional. Hadi Hariri

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Programming Languages

Introduction to Functional Programming in Haskell 1 / 56

Programming Languages and Techniques (CIS120)

Functional Programming Concepts for Data Processing

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

CSC324 Functional Programming Efficiency Issues, Parameter Lists

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

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

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

Functional abstraction

Principles of Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CS457/557 Functional Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Module 8: Local and functional abstraction

Functional Programming. Pure Functional Programming

Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

JVM ByteCode Interpreter

CS 61A Discussion 8: Scheme. March 23, 2017

CS 11 Haskell track: lecture 1

Haskell 98 in short! CPSC 449 Principles of Programming Languages

CS 320 Midterm Exam Solution

CS 314 Principles of Programming Languages. Lecture 16

Environments

INTRODUCTION TO HASKELL

CIS 500 Software Foundations Fall September 25

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

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

Mutable References. Chapter 1

CS 320 Homework One Due 2/5 11:59pm

Chapter 15. Functional Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Functional Programming Languages (FPL)

Logic - CM0845 Introduction to Haskell

Functional Programming. Pure Functional Languages

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

CSCC24 Functional Programming Scheme Part 2

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Random Testing in 321

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

Type Systems Winter Semester 2006

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

SCHEME AND CALCULATOR 5b

Functional Languages. Hwansoo Han

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Shell CSCE 314 TAMU. Haskell Functions

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Functional Programming. Big Picture. Design of Programming Languages

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Programming Languages Fall Prof. Liang Huang

CS 320 Midterm Exam. Fall 2018

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Working with recursion

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

CS558 Programming Languages

Lecture #3: Environments

Compiler principles, PS1

Lambda Calculus. Variables and Functions. cs3723 1

Programming with Math and Logic

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

To figure this out we need a more precise understanding of how ML works

An introduction introduction to functional functional programming programming using usin Haskell

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS 320: Concepts of Programming Languages

COP4020 Spring 2011 Midterm Exam

Structure and Interpretation of Computer Programs

Introduction to Denotational Semantics

Functional Programming in Haskell for A level teachers

Principles of Programming Languages 2017W, Functional Programming

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

Scheme as implemented by Racket

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Transcription:

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

The lambda calculus <expr> = ID (λ ID. <expr>) (<expr> <expr>)

The lambda calculus (Racket) <expr> = ID (lambda (ID) <expr>) (<expr> <expr>)

The lambda calculus (Haskell) <expr> = ID \ ID -> <expr> <expr> <expr>

The lambda calculus (Python) <expr> = ID lambda ID : <expr> <expr> ( <expr> )

Functional programming a programming paradigm centred on evaluating functions

Question: What is a program? Simplest answer: a single expression. <prog> = <expr> <expr> =...

What does it mean to run such a program? To evaluate the expression. <prog> = <expr> <expr> =...

Semantics the meaning of the elements of a language

Denotational semantics the abstract mathematical value of an expression [intuitively, based on our knowledge of abstract domains, e.g. arithmetic]

Operational semantics the rules that govern how an expression is evaluated [based on our model of how computation occurs]

10 3 + 7 (* 2 5) (\x -> x + 3) 7 (first (list 10 20 30 40)) ord( \n ) head (tail [9, 10, 11, 12])

In the lambda calculus, the denotational semantics use just one idea: function calls are evaluated using substitution. ((λ x. x) y) ==> y

In our limited set of Racket/Haskell, the denotational semantics use two ideas: function calls as substitution, and known operations on primitive data types. ((lambda (x) (+ x 10)) 20) ==> (+ 20 10) ==> 30

The operational semantics may seem straightforward ( call stack ) more on this throughout the course.

In the lambda calculus, the denotational semantics use just one idea: function calls are evaluated using substitution. The only thing a function can do is return a value. No mutation, no I/O.

Functional programming a programming paradigm centred on evaluating mathematical (or pure) functions (as a consequence, values are immutable)

Name binding an association of an identifier to an expression

In Racket: <binding> = (define ID <expr>) In Haskell: <binding> = ID = <expr>

a program is an expression to be evaluated, but we can include name bindings for readability <prog> = <binding>... <expr> <binding> =... <expr> =...

In our limited set of Racket/Haskell, the denotational semantics use four ideas: - function calls as substitution - known operations on primitive data types - name bindings (definitions) - name lookup

in pure functional programming, bindings are fixed, and cannot be reassigned names are referentially transparent: they can be replaced by their corresponding value everywhere in the program without changing the program s meaning

(define nums (list 1 2 3)) ; lots of code (f nums) ; lots of code (g nums) nums = [1, 2, 3] # lots of code f(nums) # lots of code g(nums)

(define nums (list 1 2 3)) ; lots of code (f (list 1 2 3)) ; lots of code (g (list 1 2 3)) nums = [1, 2, 3] # lots of code f([1, 2, 3]) # lots of code g([1, 2, 3])

unbounded data a review of structural recursion

A natural number is: - 0-1 + n, where n is a nat. A list is: - empty - x + L, where x is a value and L is a list.

structure of data -> structure of code

A generic template (define (f lst) (if (empty? lst)... (... (first lst)... (f (rest lst))...))) f lst = if null lst then... else... (head lst)... (f (rest lst))...

Pattern-matching: value-based matching

f x = if x == 0 then 10 else if x == 1 then 20 else x + 30 f 0 = 10 f 1 = 20 f x = x + 30

Pattern-matching: structural matching

g lst = if null lst then 10 else let x = head lst xs = tail lst in x + length xs g [] = 10 g (x:xs) = x + length xs

Recursion, efficiency, and the difference between interface and implementation

(define (f n) (if (zero? n) 0 (+ 1 (f (- n 1))) ))

(define (f n) (if (zero? n) 0 (f (- n 1)) ))

Let E be an expression, and E be a subexpression in E. E is in a tail position with respect to E if evaluating E is the last step in evaluating E. If E is a function call in tail position, it is a tail call.

Tail call elimination An optimization that removes (i.e., deallocates) the current stack frame when a tail call is made.

Tail recursion A recursive function is tail recursive when all recursive calls are tail calls.