CS 314 Principles of Programming Languages. Lecture 16

Similar documents
CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

Functional Programming. Pure Functional Languages

CS61A Midterm 2 Review (v1.1)

Functional Programming. Pure Functional Programming

CS 314 Principles of Programming Languages. Lecture 21

User-defined Functions. Conditional Expressions in Scheme

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

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

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

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

Documentation for LISP in BASIC

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

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

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

Building a system for symbolic differentiation

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

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

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

Programming Languages

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

An Explicit-Continuation Metacircular Evaluator

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

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

Functional Programming - 2. Higher Order Functions

CS 314 Principles of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Scheme: Strings Scheme: I/O

Lisp. Versions of LISP

A Brief Introduction to Scheme (II)

Discussion 12 The MCE (solutions)

CS 314 Principles of Programming Languages. Lecture 9

Turtles All The Way Down

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

Comp 311: Sample Midterm Examination

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

CS 314 Principles of Programming Languages. Lecture 13

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

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?

Principles of Programming Languages 2017W, Functional Programming

Sample Final Exam Questions

SCHEME AND CALCULATOR 5b

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

Building a system for symbolic differentiation

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

Organization of Programming Languages CS3200/5200N. Lecture 11

An Explicit Continuation Evaluator for Scheme

Project 2: Scheme Interpreter

(scheme-1) has lambda but NOT define

FP Foundations, Scheme

Introduction to Scheme

Scheme Quick Reference

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

COP4020 Programming Assignment 1 - Spring 2011

Administrivia. Simple data types

Functional Languages. Hwansoo Han

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

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

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

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

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

Scheme Quick Reference


Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

LECTURE 16. Functional Programming

YOUR NAME PLEASE: *** SOLUTIONS ***

CSC 533: Programming Languages. Spring 2015

Functional Programming

Typed Racket: Racket with Static Types

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

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

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

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

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

An Introduction to Scheme

Functional Programming Lecture 1: Introduction

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

Class Information ANNOUCEMENTS

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

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

Structure and Interpretation of Computer Programs

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

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

CSc 520 Principles of Programming Languages

Introduction to Functional Programming and basic Lisp

CSE413 Midterm. Question Max Points Total 100

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

CS 275 Name Final Exam Solutions December 16, 2016

Lecture Notes on Lisp A Brief Introduction

Transcription:

CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University

Class Information Reminder: midterm exam this coming Wednesday class (11/2). Homework 6 posted, due Saturday 11/5 11:55pm. Zheng Zhang 2 CS@Rutgers University

Special (Primitive) Functions eq?: identity on names (atoms) null?: is list empty? car: selects first element of list (contents of address part of register) cdr: selects rest of list (contents of decrement part of register) (cons element list): constructs lists by adding element to front of list quote or : produces constants Zheng Zhang 3 CS@Rutgers University

Other Functions + - * / numeric operators, e.g., (+ 5 3) = 8, (- 5 3) = 2 (* 5 3) = 15, (/ 5 3) = 1.6666666 = < > comparison operators for numbers Explicit type determination and test functions: All return Boolean values: #f and #t (number? 5) evaluates to #t (zero? 0) evaluates to #t (symbol? sam) evaluates to #t (list? (a b)) evaluates to #t (null? ()) evaluates to #t Note: SCHEME is a strongly typed language. Zheng Zhang 4 CS@Rutgers University

Other Functions (number? sam) evaluates to #f (null? (a)) evaluates to #f (zero? (- 3 3)) evaluates to #t (zero? (- 3 3)) type error (list? (+ 3 4)) evaluates to #f (list? (+ 3 4)) evaluates to #t Zheng Zhang 5 CS@Rutgers University

READ-EVAL-PRINT Loop The Scheme interpreters on the ilab machines are called mzscheme, racket, and drracket. drracket is an interactive environment, the others are command-line based. For example: Type mzscheme, and you are in the READ-EVAL-PRINT loop. Use Control D to exit the interpreter. READ: Read input from user: a function application EVAL: Evaluate input: (f arg 1 arg 2...arg n) 1. evaluate f to obtain a function 2. evaluate each arg i to obtain a value 3. apply function to argument values PRINT: Print resulting value: the result of the function application You can write your Scheme program in file <name>.ss and then read it into the Scheme interpreter by saying at the interpreter prompt: (load "<name>.ss") Zheng Zhang 6 CS@Rutgers University

READ-EVAL-PRINT Loop Example > (cons a (cons b (c d))) (a b c d) 1. Read the function application (cons a (cons b (c d))) 2. Evaluate cons to obtain a function 3. Evaluate a to obtain a itself 4. Evaluate (cons b (c d)): 4.1 Evaluate cons to obtain a function 4.2 Evaluate b to obtain b itself 4.3 Evaluate (c d) to obtain (c d) itself 4.4 Apply the cons function to b and (c d) to obtain (b c d) 5. Apply the cons function to a and (b c d) to obtain (a b c d) 6. Print the result of the application: (a b c d) Zheng Zhang 7 CS@Rutgers University

Quotes Inhibit Evaluation ;;Same as before: > (cons a (cons b (c d))) (a b c d) ;;Now quote the second argument: > (cons a (cons b (c d))) (a cons (quote b) (quote (c d))) ;;Instead, un-quote the first argument: > (cons a (cons b (c d))) ERROR: unbound variable: a Zheng Zhang 8 CS@Rutgers University

Defining Global Variables The define constructs extends the current interpreter environment by the new defined (name, value) association. > (define foo (a b c)) #<unspecified> > (define bar (d e f)) #<unspecified> > (append foo bar) (a b c d e f) > (cons foo bar) ((a b c) d e f) > (cons foo bar) (foo d e f) Zheng Zhang 9 CS@Rutgers University

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): (define atom? (lambda (object) (not (pair? object)))) 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) Evaluates to #t Evaluates to #f Evaluates to #f Zheng Zhang 10 CS@Rutgers University

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) ()))) Zheng Zhang 11 CS@Rutgers University

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 true value 2. Evaluate and return the corresponding result 3. If none of the conditions returns a true value, evaluate and return <else-result> Zheng Zhang 12 CS@Rutgers University

Conditional Execution: cond (define abs-val (lambda (x) (cond ((>= x 0) x) (else (- x))))) (define rest-if-first (lambda (e l) (cond ((null? l) ()) ((eq? e (car l)) (cdr l)) (else ())))) Zheng Zhang 13 CS@Rutgers University

Recursive Scheme Functions: Abs-List (abs-list (1-2 -3 4 0)) (1 2 3 4 0) (abs-list ()) () (define abs-list (lambda (l) (if (null? l) () (cons (abs-val (car l)) (abs-list (cdr l))) ) ) ) Zheng Zhang 14 CS@Rutgers University

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))))) ) Zheng Zhang 15 CS@Rutgers University

Equality Checking The eq? predicate doesn t work for lists. Why not? 1. (cons a ()) produces a new list 2. (cons a ()) produces another new list 3. eq? checks if its two arguments are the same 4. (eq? (cons a ()) (cons a ())) evaluates to #f 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 lists, is called a pair. pair? car cdr Symbols are stored uniquely, so eq? works on them. Zheng Zhang 16 CS@Rutgers University

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 Zheng Zhang 17 CS@Rutgers University

Scheme: Functions as 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 x)) 3) ((lambda (x) (* 2 x)) 3) (* 2 3) 6 REMINDER: 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 arguments 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) Zheng Zhang 18 CS@Rutgers University

Higher-order Functions (Cont.) Functions as returned values: (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) 11 Zheng Zhang 19 CS@Rutgers University

Next Lecture Reading: Scott Chapter 11.1 to 11.3 More Scheme and higher-order functions Zheng Zhang 20 CS@Rutgers University