FP Foundations, Scheme

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

Functional Programming. Big Picture. Design of Programming Languages

Chapter 15 Functional Programming Languages

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Organization of Programming Languages CS3200/5200N. Lecture 11

Functional Programming

Chapter 15. Functional Programming Languages

Imperative, OO and Functional Languages A C program is

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

Imperative languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Functional Programming. Pure Functional Programming

User-defined Functions. Conditional Expressions in Scheme

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

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

Functional Languages. Hwansoo Han

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

Chapter 15. Functional Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CS 314 Principles of Programming Languages

CSC 533: Programming Languages. Spring 2015

Chapter 15. Functional Programming Languages ISBN

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

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

LECTURE 16. Functional Programming

4/19/2018. Chapter 11 :: Functional Languages

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

Functional Programming

Lambda Calculus see notes on Lambda Calculus

Functional Programming Languages (FPL)

Documentation for LISP in BASIC

Lambda Calculus. Gunnar Gotshalks LC-1

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Programming Languages

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

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

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

Example Scheme Function: equal

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

CS 314 Principles of Programming Languages

An introduction to Scheme

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

15. Functional Programming

Functional programming with Common Lisp

Scheme Quick Reference

An Introduction to Scheme

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

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

SOFTWARE ARCHITECTURE 6. LISP

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

Functional programming in LISP

CSCI337 Organisation of Programming Languages LISP

Project 2: Scheme Interpreter

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

SCHEME AND CALCULATOR 5b

Evaluating Scheme Expressions

Programming Languages

Introductory Scheme. Revision 1

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

Functional Programming Languages (FPL)

Scheme Quick Reference

Lambda Calculus LC-1

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

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Lisp. Versions of LISP

CSCC24 Functional Programming Scheme Part 2

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Announcements. Today s Menu

A Brief Introduction to Scheme (II)

Introduction to Scheme

Concepts of Programming Languages

COP4020 Programming Assignment 1 - Spring 2011

Recursive Functions of Symbolic Expressions and Their Application, Part I

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

Scheme: Strings Scheme: I/O

Lecture Notes on Lisp A Brief Introduction

CS 314 Principles of Programming Languages. Lecture 16

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Control in Sequential Languages

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

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and 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.

Turtles All The Way Down

Chapter 1. Fundamentals of Higher Order Programming

CSc 520 Principles of Programming Languages

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

Introduction to lambda calculus Part 3

Lisp Basic Example Test Questions

CS 360 Programming Languages Interpreters

A Brief Introduction to Scheme (I)

Functional Programming - 2. Higher Order Functions

CSc 520 Principles of Programming Languages

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

Review of Functional Programming

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

Common LISP Tutorial 1 (Basic)

CS61A Midterm 2 Review (v1.1)

John McCarthy IBM 704

Transcription:

FP Foundations, Scheme In Text: Chapter 15 1

Functional Programming -- Prelude We have been discussing imperative languages C/C++, Java, Fortran, Pascal etc. are imperative languages Imperative languages follow the von Neumann architecture Memory CPU There is a different way of looking at things Functional/Applicative programming Based on Mathematics; Math functions 2

Functional Programming -- Mathematical Foundations A mathematical function is a mapping of members of one set to another set The input set is called the domain The output set is called the range function definition -- usual form: cube(x = x * x * x, where x is a real number then, e.g., cube(2 = 8 Lambda notation (Alonzo Church, 1941 provides for nameless functions: (x x * x * x Can apply just like a named function: (x x * x * x (2 = 8 Functions do not have state; they are side-effect free 3

Functional Programming -- Mathematical Foundations Functional Forms / Higher Order Functions function composition: has two function parameters, yields a function whose value is the first function applied to the result of the second h = f ο g -- means apply g first, then apply f to the result example: if f(x = x + 2 g(y = 3 * y then h(z = f(g(z = (3 * z + 2 construction: takes a list of functions and applies each in turn to the argument, creating a list of results written by enclosing function names in brackets, e.g. [g,h,i] example: if g(x = x * x h(x = 2 * x i(x = x / 2 then [g, h, i] (4 yields (16, 8, 2 4

Functional Programming -- Mathematical Foundations Functional Forms apply-to-all: takes a single function and applies it to a list of arguments, creating a list of values denoted by α example: if h(x = x * x then α(h, (2, 3, 4 yields (4, 9, 16 5

Functional Programming LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the first has imperative features, but we will discuss a subset that is purely functional Functional subset No assignment statement No looping statement Recursion recursion recursion 6

LISP Data Types Data Types: everything in LISP are S-expressions Atoms : identifiers, numbers, symbols a 100 + Lists ( a b c d ( a ( b c d e All data structures in Lisp are single-linked lists each node has 2 pointers, one to element, the other to next node in the list 7

Data Structures Single atom: atom List of atoms: ( a b c a b c List containing list: ( a ( b c d a d b c 8

LISP Primitives Primitive numeric functions +, -, *, / 42 => 42 (* 3 6 => 18 (+ 1 2 3 => 6 (sqrt 16 => 4 Numeric Predicate Functions Predicate Functions return true (#t, or false (nil ( =, <, >, >=, <=, <>, even? odd? zero? ( = 16 16 => #t ( even? 29 => ( ( > 10 ( * 2 4 => #t ( zero? ( - 10 ( * 2 5 => #t 9

Defining functions ( define (function_name arg1 arg2 argn Sexp ( define ( square x (* x x ( define ( hypotenuse side1 side2 ( sqrt (+ ( square side1 ( square side2 You can also define named constants: ( define pie 3.14159 ( define (circumference r (* 2 pie r 10

Control Flow Control flow is modeled after math functions f (x = 1if x = 0 x * f (x 1 if x > 0 ( if predicate then_expr else_expr ( define ( factorial x ( if ( = x 0 1 ( * x (factorial (- x 1 ( cond ( pred1 expr ( pred2 expr ( predn expr ( else expr ( define ( factorial x ( cond ( < x 0 ( ( = x 0 1 ( > x 0 (* x (factorial (- x 1 11

List Functions quote => ' ( quote a => 'a = a ( quote ( a b c => '(a b c = (a b c car : List => Sexp One input arg => List Returns first element of that list ( car '( a b => a ( car '(( a b c => ( a b ( car '( a ( b c => a ( car 'a => undefined ( car '( => undefined 12

List Functions cdr : List => List One input arg => List Returns list of all elements but the first element (cdr '(a b c => (b c (cdr '((a b (c => ((c (cdr '(a => ( (cdr 'a => undefined (cdr '( => undefined (cdr '55 => undefined 13

List Functions cons : Sexp X List => List 2 args as input: ( cons a1 a2 a1 : Sexp a2 : List Returns a2 with a1 inserted as its first element (cons 'a '(b c => (a b c (cons 'a '( => (a (cons '(a b '((c d e => ((a b (c d e Be careful, cons will take non list (atom arguments and form "dotted" pairs, e.g. (cons 'a 'b => (a. b Note: (cons? X is a predicate form in Dr. Scheme and returns true if x is a list 14

More Predicates the following return #t if the arguments are of the indicated type, and nil ( otherwise (symbol? 'a => #t (symbol? '( => ( (symbol? 55 => ( (number? '55 => #t (number? 55 => #t (number? (a => ( LIST?: Dr. Scheme (cons? '(a => #t (cons? 'a => ( (cons? '( => #t (null? '( => #t (null? '(a c => ( 15

More Predicates eq? Sexp X Sexp => { #t, ( } Returns true if objects are equal through pointer comparison. Guaranteed to work on symbols equal? Sexp X Sexp => {#t, ( } Recursively compares two objects to determine if they are equal (works on symbols, atoms, numbers, and lists 16

Examples How do we implement equal? (define (atom? atm (cond ((cons? atm (null? atm ((symbol? atm #t ((number? atm #t (else ( (define (equal? lis1 lis2 (cond ((atom? lis1 (eq? lis1 lis2 ((atom? lis2 ( ((equal? (car lis1 (car lis2 (equal? (cdr lis1 (cdr lis2 (else ( 17

More Examples (define (member? atm lis (cond ((null? lis ( ((eq? atm (car lis #t (else (member? atm (cdr lis (define (append lis1 lis2 (cond ((null? lis1 lis2 (else (cons (car lis1 (append (cdr lis1 lis2 18

Additional Functions Additional control flow primitives that are available: (if Sexp 1 Sexp 2 [Sexp 3 ] if (Sexp 1 then Sexp 2 [else Sexp 3 ] (while Sexp 1 Sexp 2 while (Sexp 1 do (Sexp 2 od Blocking primitive: (begin Sexp 1 Sexp 2... Sexp n Variable initialization primitive: (set! x Sexp USE THESE FOR TESTING PURPOSES ONLY! 19

Lambda Expressions Intuitively, lambda expressions allow one to define and use nameless functions and to pass them to be used in other functions (lambda (lis (car (cdr lis Given to a lisp interpreter, the above function definition returns the second element in a list, e.g. ((lambda (lis (car (cdr lis '(a b c returns "b" 20

Lambda Expressions We CAN integrate the lambda expression into a function definition: (define second (lambda (lis (car (cdr lis Once "evaled" by the interpreter, the function definition is "bound" to the name "second" such that (second '(a b c => b But, our "standard" way of defining functions will work too... (define (second lis (car (cdr lis (second '(a b c => b SO, WHAT DOES THE LAMBDA EXPRESSION BUY US? 21

Lambda Expressions WE NOW HAVE THE CAPABILITY TO PASS FUNCTION DEFINITIONS AS PARAMETERS! Suppose that we want to write an "apply-to-all" function that takes a function definition and list as its arguments and applies its function argument to all elements in the list. (define (mapcar fctn lis (cond ((null? lis ( (else (cons (fctn (car lis (mapcar fctn (cdr lis (mapcar (lambda (num (* num num '(2 4 6 returns a list containing the square of all elements in the original list, i.e., (4 16 36 22

Lambda Expressions Why not simply define a function f that performs a specified operation on one element and then pass IT to mapcar, e.g. (define (square x (* x x (define (mapcar fctn lis (cond ((null? lis ( (else (cons (fctn (car lis (mapcar fctn (cdr lis (mapcar square '(2 4 6???? 23

Scoping in LISP Lisp allows programs to reference "unbound" variables, e.g. (define (f atm (cons atm y ; y is an unbound variable What are the implications of this capability with respect to scoping? 24

Scoping in LISP In reality, Lisp does allow one to define global and local variables, e.g. If (set!... inside a (define x 5 ; Global x function defn (set! x (car '(a b c ; Gbl/Lcl x If (set!... outside a function defn In reality, Lisp does allow programs to reference "unbound" variables, e.g. (define (f atm (cons x y ; y is an unbound variable ; x assumes the prev (set! x... What are the implications of these capabilities with respect to scoping? 25

Scoping in LISP Consider the following example: (define (A...... (car X... ; unbound ref (define (B Fctn X... Fctn... ; invoke Fctn * (define (C X Z... A... ; invoke fctn A **... (B A Z... ; invoke fctn B *** 26

Scoping in LISP Assuming that "our" Lisp is statically scoped (and most current Lisps are statically scoped, let's consider the impact of the following invocation of C: (C (i j (k l m What is the binding of X in A after being invoked at **? What is the binding of X in A after being sent to B through the call *** and being invoked at *? Is it what you expected? What is needed is the ability to bind an execution environment at same time a function is passed as a parameter! (Funarg Problem Solution:... (B (function A Z... 27

SCOPING IN LISP Consider the same scenario again: (define (A...... (car X... (define (B Fctn X... Fctn... (define (C X Z... A...... (B (function A Z... In Pascal, static scoping and lexical scoping are effectively synonymous. Although we are able to achieve "static" scoping through the use of the function primitive, is this also a "lexical" scoping? 28

SCHEME SCHEME is a dialect of LISP SCHEME extends LISP Statically scoped Minor syntax changes - will not affect us Has extensions - additional functions we will not use We will use only the pure LISP parts of SCHEME 29

A Little Help Access Help Desk Start => Programs => PTL Scheme => Help Desk Software Tour Manuals 30