CS 314 Principles of Programming Languages

Similar documents
CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

CS 314 Principles of Programming Languages

Documentation for LISP in BASIC

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Programming

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

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

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

Functional Programming. Big Picture. Design of Programming Languages

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

Organization of Programming Languages CS3200/5200N. Lecture 11

Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Chapter 15. Functional Programming Languages

Introduction to Scheme

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

LECTURE 16. Functional Programming

Functional Programming Lecture 1: Introduction

CSc 520 Principles of Programming Languages

Scheme: Expressions & Procedures

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

FP Foundations, Scheme

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

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

Programming Languages

Chapter 15 Functional Programming Languages

A Small Interpreted Language

Modern Programming Languages. Lecture LISP Programming Language An Introduction

User-defined Functions. Conditional Expressions in Scheme

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

Scheme Datatypes. expressions. pairs. atoms. lists. symbols. booleans

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

CSC 533: Programming Languages. Spring 2015

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

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

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

Principles of Programming Languages 2017W, Functional Programming

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

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?

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

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

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

Symbolic Computation

CS 360 Programming Languages Interpreters

Lisp. Versions of LISP

Functional Programming Languages (FPL)

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

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

Heap storage. Dynamic allocation and stacks are generally incompatible.

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

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

SCHEME AND CALCULATOR 5b

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

A Brief Introduction to Scheme (II)

Chapter 15. Functional Programming Languages ISBN

Functional Languages. Hwansoo Han

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

Imperative languages

Introductory Scheme. Revision 1

COP4020 Programming Assignment 1 - Spring 2011

SOFTWARE ARCHITECTURE 6. LISP

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

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

Programming Systems in Artificial Intelligence Functional Programming

Principles of Programming Languages

Lambda Calculus. Gunnar Gotshalks LC-1

Imperative, OO and Functional Languages A C program is

Example Scheme Function: equal

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

Turtles All The Way Down

ALISP interpreter in Awk

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Control in Sequential Languages

Scheme: Strings Scheme: I/O

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

Introduction to lambda calculus Part 3

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

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Comp 311: Sample Midterm Examination

A Brief Introduction to Scheme (I)

CSCC24 Functional Programming Scheme Part 2

Functional Programming - 2. Higher Order Functions

CS61A Midterm 2 Review (v1.1)

Scheme Quick Reference

Functional programming in LISP

Lambda Calculus see notes on Lambda Calculus

An Introduction to Scheme

Functional Programming

Functional programming with Common Lisp

Discussion 12 The MCE (solutions)

CSc 520 Principles of Programming Languages

1.3. Conditional expressions To express case distinctions like

Intro. Scheme Basics. scm> 5 5. scm>

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Introduction to Functional Programming

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Transcription:

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

Review: Computation Paradigms Functional: Composition of operations on data. No named memory locations Value binding through parameter passing Key operations: Function application and Function abstraction Basis in lambda calculus 2

Review: Pure Functional Languages Fundamental concept: application of (mathematical functions to values 1. Referential transparency: the value of a function application is independent of the context in which it occurs value of foo(a, b, c depends only on the values of foo, a, b and c it does not depend on the global state of the computation all vars in function must be local (or parameters 2. The concept of assignment is NOT part of function programming no explicit assignment statements variables bound to values only through the association of actual parameters to formal parameters in function calls thus no need to consider global states 3

Review: Pure Functional Languages 3. Control flow is governed by function calls and conditional expressions no loop recursion is widely used 4. All storage management is implicit needs garbage collection 5. Functions are First Class Values can be returned from a subroutine can be passed as a parameter can be bound to a variable 4

Review: Pure Functional Languages A program includes: 1. A set of function definitions 2. An expression to be evaluated E.g. in scheme, > (define length (lambda (x (if (null? x 0 (+ 1 ( length (rest x > (length '(A LIST OF 7 THINGS 5 5

LISP Functional language developed by John McCarthy in the mid 50 s Semantics based on Lambda Calculus All functions operate on lists or symbols called: S-expression Only five basic functions: list functions con, car, cdr, equal, atom, & one conditional construct: cond Useful for list-processing (LISP applications Program and data have the same syntactic form S-expression Originally used in Artificial Intelligence 6

SCHEME Developed in 1975 by Gerald J. Sussman and Guy L. Steele A dialect of LISP Simple syntax, small language Closer to initial semantics of LISP as compared to COMMON LISP Provide basic list processing tools Allows functions to be first class objects 7

SCHEME Expressions are written in prefix, parenthesized form (function arg1 arg2 argn (+ 4 5 (+ (* 3 4 (- 5 3 Operational semantics: In order to evaluate an expression 1. Evaluate function to a function value 2. Evaluate each argi in order to obtain its value 3. Apply function value to these values 8

S-expression S-expression ::= Atom ( S-expression S-expression S-expression Atom ::= Name Number #t #f ε #t ( (a b c (a (b c d ((a b c (d e (f (1 (b 2 Lists have nested structure! 9

Lists in Scheme The building blocks for lists are pairs or cons-cells. Lists use the empty list ( as an end-of-list marker. (a b (a (b c (d e a a b ( (a. b b c ( d ( a b e ( 10

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. 11

Special (Primitive Functions '( is an empty list (car '(a b c = (car '((a b (c d = (cdr '(a b c = (cdr '((a b (c d = a ( a (b c (b (c d 12

Special (Primitive Functions car and cdr can break up any list: (car (cdr (cdr '((a b (c d = (c d (cdr '((a b (c d = (b (c d cons can construct any list: (cons 'a '( = (a (cons 'd '(e = ( d e (cons '(a b '(c d = ((a b c d (cons '(a b c '((a b = ((a b c (a b 13

Other Functions +, -, *, / numeric operators, e.g., (+ 5 3 = 8, (- 5 3 = 2 (* 5 3 = 15, (/ 5 3 = 1.6666666 = < > comparisons for numbers Explicit type determination and type 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. 14

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 15

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 racket, and you are in the READ-EVAL PRINT loop. Use Control D to exit the interpreter. 16

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. READ: Read input from user: A function application EVAL: Evaluate input: (f arg1 arg2 argn 1. evaluate function to a function value 2. evaluate each argi in order to obtain its value 3. apply function value to these values PRINT: Print resulting value: The result of function application You can write your Scheme program in file <name>.rkts and then read it into the Scheme interpreter by saying at the interpreter prompt: (load "<name>.rkts" 17

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 (i Evaluate cons to obtain a function (ii Evaluate 'b to obtain b itself (iii Evaluate '(c d to obtain (c d itself (iv Apply cons function to b and (c d to obtain (b c d 5. Apply 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 18

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 19

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 20

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 '( 21

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> 22

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 23

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 '( ( 24

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 25

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 26