CS 314 Principles of Programming Languages

Similar documents
CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

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

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Lisp. Versions of LISP

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

User-defined Functions. Conditional Expressions in Scheme

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

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

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Building a system for symbolic differentiation

Documentation for LISP in BASIC

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Project 2: Scheme Interpreter

A Brief Introduction to Scheme (II)

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme Quick Reference

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Scheme: Strings Scheme: I/O

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Scheme Quick Reference

Functional Programming - 2. Higher Order Functions

Functional Programming. Pure Functional Programming

Introduction to Scheme

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

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

YOUR NAME PLEASE: *** SOLUTIONS ***

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Building a system for symbolic differentiation

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

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

Functional programming with Common Lisp

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

An Explicit-Continuation Metacircular Evaluator

CS 314 Principles of Programming Languages

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

CS61A Midterm 2 Review (v1.1)

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

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

Scheme Basics > (butfirst '(help!)) ()

An introduction to Scheme

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

Chapter 15 Functional Programming Languages

Typed Scheme: Scheme with Static Types

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Administrivia. Simple data types

Organization of Programming Languages CS3200/5200N. Lecture 11

An Introduction to Scheme

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

1.3. Conditional expressions To express case distinctions like

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Programming Systems in Artificial Intelligence Functional Programming

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

Problem 1: Binary Trees: Flatten


Principles of Programming Languages 2017W, Functional Programming

Programming Languages

FP Foundations, Scheme

Turtles All The Way Down

Rewriting your function using map and foldr

Functional Languages. Hwansoo Han

Functional Programming. Big Picture. Design of Programming Languages

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

LECTURE 16. Functional Programming

Heap storage. Dynamic allocation and stacks are generally incompatible.

Sample Final Exam Questions

Chapter 15. Functional Programming Languages

Functional Programming Lecture 1: Introduction

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

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

CPS 506 Comparative Programming Languages. Programming Language Paradigm

SCHEME AND CALCULATOR 5b

Typed Racket: Racket with Static Types

Recursion & Iteration

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Midterm Examination (Sample Solutions), Cmput 325

Comp 311: Sample Midterm Examination

CSC 533: Programming Languages. Spring 2015

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Lecture Notes on Lisp A Brief Introduction

C311 Lab #3 Representation Independence: Representation Independent Interpreters

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Introduction to Functional Programming

Functional Programming

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

CS 275 Name Final Exam Solutions December 16, 2016

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Simplifying Logical Formulas

Transcription:

CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018

Class Information Homework 6 will be posted later today. All test cases for project 1 have been released. If a small issue in project 1 results in your code not working for most test cases, you can submit a patch (using the linux utility tool patch to generate the difference of two versions of a file by the end of this week. We will review the change before we determine if you will get partial credit back. 2

Special (Primitive Functions eq?: identity on names (atoms null?: is list empty? car: select first element of the list (contents of address part of register cdr: select rest of the list (contents of decrement part of register (cons element list: constructs lists by adding element to the front of list quote or ': produces constants Do not evaluate the ' the content after '. Treat them as list of literals. 3

Quotes Inhibit Evaluation > ( cons 'a (cons 'b '(c d (a b c d ;; Now if we quote the second argument > ( cons 'a '(cons 'b '(c d (a cons 'b '(c d ;; If we unquote the first argument > ( cons a (cons 'b '(c d a: undefined; cannot reference undefined identifier context 4

Review: Defining Scheme Functions (define <fcn-name> (lambda (<fcn-params> <expression> Example: Given function pair? (true for non-empty lists, false o/w and function not (boolean negation: Evaluating (atom? '(a: 1. Obtain function value for atom? 2. Evaluate '(a obtaining (a 3. Evaluate (not (pair? object a Obtain function value for not b Evaluate (pair? object i. Obtain function value for pair? ii. Evaluate object obtaining (a iii. Evaluates to #t c Evaluates to #f 4. Evaluates to #f (define atom? ( lambda (object (not (pair? object 5

Review: Conditional Execution: if (if <condition> <result1> <result2> 1. Evaluate <condition> 2. If the result is a true value (i.e., anything but #f, then evaluate and return <result1> 3. Otherwise, evaluate and return <result2> (define abs-val ( lambda (x ( if ( >= x 0 x (- x (define rest-if-first ( lambda (e l ( if ( eq? e (car l ( cdr l '( 6

Review: Conditional Execution cond (cond (<condition1> <result1> (<condition2> <result2>... (<conditionn> <resultn> (else <else-result> ; optional else clause 1. Evaluate conditions in order until obtaining one that returns a #t value 2. Evaluate and return the corresponding result 3. If none of the conditions returns a true value, evaluate and return <else-result> 7

Conditional Execution: cond (define rest-if-first (lambda (e l (cond ( (null? l '( ( (eq? e (car l ( cdr l ( else '( Example 1 Example 2 (define abs-val (lambda ( x ( cond ( (>= x 0 x (else (- x 8

Recursive Scheme Functions: abs-list (define abs-list (lambda (l (if (null? l '( (cons (abs (car l (abs-list (cdr l (abs-list '(1-2 -3 4 0 (1 2 3 4 0 (abs-list '( ( 9

Recursive Scheme Functions: Append (append '(1 2 '(3 4 5 (1 2 3 4 5 (append '(1 2 '(3 (4 5 (1 2 3 (4 5 (append '( '(1 4 5 (1 4 5 (append '(1 4 5 '( (1 4 5 (append '( '( ( (define append (lambda (x y (cond ( (null? x y ( (null? y x ( else (cons (car x (append (cdr x y 10

Equality Checking The eq? predicate does not work for lists. Why not? (cons 'a '( produces a new list (cons 'a '( produces another new list eq? checks whether two arguments are the same (eq? (cons 'a '( (cons 'a '( evaluates to #f 11

Equality Checking Lists are stored as pointers to the first element (car and the rest of the list (cdr. This elementary data structure, the building block of a list, is called a pair car cdr Symbols are stored uniquely, so eq? works on them. 12

Equality Checking for Lists For lists, need a comparison function to check for the same structure in two lists. (define equal? (lambda (x y (or ( and (atom? x (atom? y (eq? x y ( and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y 13

Equality Checking for Lists For lists, need a comparison function to check for the same structure in two lists. (define equal? (lambda (x y (or ( and (atom? x (atom? y (eq? x y ( and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y 14

Equality Checking for Lists For lists, need a comparison function to check for the same structure in two lists. (define equal? (lambda (x y (or ( and (atom? x (atom? y (eq? x y ( and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y 15

Equality Checking for Lists For lists, need a comparison function to check for the same structure in two lists. (define equal? (lambda (x y (or ( and (atom? x (atom? y (eq? x y ( and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y 16

Equality Checking for Lists For lists, need a comparison function to check for the same structure in two lists. (define equal? (lambda (x y (or ( and (atom? x (atom? y (eq? x y ( and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y 17

Equality Checking for Lists For lists, need a comparison function to check for the same structure in two lists. (define equal? (lambda (x y (or ( and (atom? x (atom? y (eq? x y ( and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y 18

Equality Checking for Lists For lists, need a comparison function to check for the same structure in two lists. (define equal? (lambda (x y (or ( and (atom? x (atom? y (eq? x y ( and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y 19

Equality Checking for Lists For lists, need a comparison function to check for the same structure in two lists. (define equal? (lambda (x y (or ( and (atom? x (atom? y (eq? x y ( and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y (equal? 'a 'a evaluates to #t (equal? 'a 'b evaluates to #f (equal? '(a '(a evaluates to #t (equal? '((a '(a evaluates to #f 20

Scheme: Functions as First Class Values Functions as arguments: (define f (lambda (g x (g x (f number? 0 (number? 0 #t (f length '(1 2 (length '(1 2 2 (f (lambda (x (* 2 3 3 ((lambda (x (* 2 3 3 (* 2 3 6 21

Scheme: Functions as First Class Values (Higher-Order Functions as arguments: (define f (lambda (g x (g x (f number? 0 (number? 0 #t (f length '(1 2 (length '(1 2 2 (f (lambda (x (* 2 3 3 ((lambda (x (* 2 3 3 (* 2 3 6 22

Scheme: Functions as First Class Values (Higher-Order Computation, i.e., function application is performed by reducing the initial S-expression (program to an S-expression that represents a value. Reduction is performed by substitution, i.e., replacing formal by actual parameters in the function body. Examples for S-expressions that directly represent values, i.e., cannot be further reduced: function values (e.g.: (lambda (x e constants (e.g.: 3, #t Computation completes when S-expression cannot be further reduced. 23

Higher-Order Functions (Cont. Functions as returned value: (define plusn ( lambda (n (lambda (x (+ n x (plusn 5 evaluates to a function that adds 5 to its argument: Question: How would you write down the value of (plusn 5? ((plusn 5 6 24

Higher-Order Functions (Cont. In general, any n-ary function (lambda (x_1 x_2 x_n e can be rewritten as a nest of n unary functions: (lambda (x_1 (lambda (x_2 ( (lambda(x_n e 25

Higher-Order Functions (Cont. In general, any n-ary function (lambda (x_1 x_2 x_n e can be rewritten as a nest of n unary functions: (lambda (x_1 (lambda (x_2 ( (lambda(x_n e This translation process is called currying. It means that having functions with multiple parameters do not add anything to the expressiveness of the language: ( (lambda (x_1 x_2... x_n e v_1 v_2... v_n ( (((lambda (x_1 (lambda (x_2... (lambda (x_n e... v_1 v_2... v_n 26

Higher-order Functions: map (define map (lambda (f l (if (null? l '( ( cons ( f (car l (map f (cdr l map takes two arguments: a function and a list map builds a new list by applying the function to every element of the (old list 27

Higher-order Functions: map (define map (lambda (f l (if (null? l '( ( cons ( f (car l (map f (cdr l map takes two arguments: a function and a list map builds a new list by applying the function to every element of the (old list 28

Higher-Order Functions: map Example: (map abs '(-1 2-3 4 (1 2 3 4 (map (lambda (x (+ 1 x '(-1 2 3 (0 3 4 Actually, the built-in map can have more than two arguments: (map + '(1 2 3 '(4 5 6 (5 7 9 29

Next Lecture Things to do: Read Scott, Chapter 9.1-9.3 (4th Edition or Chapter 8.1-8.3 (3rd Edition, Chapter 11.1-11.3 (4th Edition or Chapter 10.1-10.3 (3rd Edition 30