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

Similar documents
CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Languages

Project 2: Scheme Interpreter

Functional Programming. Pure Functional Languages

CS 314 Principles of Programming Languages

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

Documentation for LISP in BASIC

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lisp. Versions of LISP

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

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Programming Languages

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 Scheme

User-defined Functions. Conditional Expressions in Scheme

Imperative languages

April 2 to April 4, 2018

FP Foundations, Scheme

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

Functional Programming

LECTURE 16. Functional Programming

CS 314 Principles of Programming Languages. Lecture 16

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

An Introduction to Scheme

Principles of Programming Languages 2017W, Functional Programming

Functional programming in LISP

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

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

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

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

CSc 520 Principles of Programming Languages

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

Functional programming with Common Lisp

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

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

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

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Scheme: Strings Scheme: I/O

Chapter 15. Functional Programming Languages

John McCarthy IBM 704

Introductory Scheme. Revision 1

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

Administrivia. Simple data types

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

COP4020 Programming Assignment 1 - Spring 2011

A Brief Introduction to Scheme (II)

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

Lecture #24: Programming Languages and Programs

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

Chapter 15 Functional Programming Languages

Example Scheme Function: equal

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

A Small Interpreted Language

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

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

Essentials of Programming Languages Language

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

Essentials of Programming Languages Language

Scheme: Expressions & Procedures

Functional Programming. Big Picture. Design of Programming Languages

CS 314 Principles of Programming Languages

An Explicit-Continuation Metacircular Evaluator

(scheme-1) has lambda but NOT define

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

CSC 533: Programming Languages. Spring 2015

Programming Languages

SOFTWARE ARCHITECTURE 6. LISP

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

CSc 520 Principles of Programming Languages

Functional Programming Lecture 1: Introduction

Scheme as implemented by Racket

Scheme Quick Reference

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

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

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

SCHEME AND CALCULATOR 5b

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

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

Concepts of programming languages

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

An Explicit Continuation Evaluator for Scheme

Review of Functional Programming

Turtles All The Way Down

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

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

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

CSE 341 Section Handout #6 Cheat Sheet

Chapter 15. Functional Programming Languages

Introduction to lambda calculus Part 3

CS450 - Structure of Higher Level Languages

Functional Languages. Hwansoo Han

Scheme Quick Reference

Principles of Programming Languages

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

INTRODUCTION TO SCHEME

Transcription:

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions can return functions Requires garbage collection 2 Lambda Calculus Mathematical functional language Language constructs Variables: x Primitive functions: * Function abstraction: (x) x*x or x.x*x Function call: f (x) or f x Example ( (x) x*x*x) (2) yields 8 3 CSC 4101: Programming Languages 1

Scheme Dialect of LISP (from MIT, ca. 1975) Lists as built-in data type Same syntax for lists and programs Functions are (first-class) data Dynamic, strong typing Interaction (read-eval-print) loop 4 Scheme Syntax S-expressions s-expression ::= literals symbols (s-expression...) Special forms for specific language constructs 5 Atomic Data Types Numbers 3.14, 42, 4101 Booleans #t, #f Characters, Strings #\a, #\newline, "Hello World!" Symbols f, x, +, *, foo, bar, null?, set! 6 CSC 4101: Programming Languages 2

Variable Definitions Variable definitions (define x 3) (define y 5) This is not an assignment! A new variable is defined Like `int x = 3; in C 7 Functions Function definition (define (double x) (+ x x)) Function application (call) (* 3 4) (+ 2 x y) (double 3) (* (+ 3 4) (double 3)) 8 Anonymous Functions Anonymous function definitions (lambda (x) (+ x x)) Variable definitions (define double (lambda (x) (+ x x))) (define * +) Function application (double 3) ((lambda (x) (+ x x)) 3) 9 CSC 4101: Programming Languages 3

Lists Quotes (quote x), x Empty list (), often assigned to the variable nil Nonempty list (1 2 3), (a b 3), (1 (2 3) 4) 10 List Representation The list (1 2) is stored as cons / \ 1 cons / \ 2 () 11 List Representation The list (1 (2 3) 4) is stored as cons / \ 1 cons / \ cons cons / \ / \ 2 cons 4 () / \ 3 () 12 CSC 4101: Programming Languages 4

List Functions Predefined functions car, cdr, cons Examples (car (1 2)) returns 1 (cdr (1 2)) returns (2) (cons 1 (2)) returns (1 2) (cons 1 2) returns (1. 2) 13 S-Expressions List Syntax S-Expression Syntax (1 2 3) (1. (2. (3. ()))) ((1 2)) ((1. (2. ())). ()) Not a list (1. 2) 14 More Examples Expression Value (- 24 (* 4 3)) 12 (car (cdr (1 2 3))) 2 (cadr (1 2 3)) 2 (cons 1 ()) (1) (cons 1 2) (1. 2) (cons () ()) (()) (car (())) () 15 CSC 4101: Programming Languages 5

More Examples Expression Value (define x 3) (+ x 2) 5 x x (double x) 6 (double x) (double x) (define double) (* (- x) 7) 42 16 More List Functions Expression Value (null? ()) #t (null? (a b)) #f (list 1) (1) (list a b c) (a b c) (append (1) (2)) (1 2) (map double (1 2 3)) (2 4 6) 17 Higher-Order Functions (define (twice f x) (f (f x))) or (define twice (lambda (f x) (f (f x)))) (twice double 2) ; returns 8 (twice (lambda (x) (* x x)) 2) ; returns 16 18 CSC 4101: Programming Languages 6

Higher-Order Functions (cont d) ;; Curried version of twice (define (twice f) (lambda (x) (f (f x)))) (define quadruple (twice double)) (quadruple 2) ; returns 8 ((twice double) 2) ; returns 8 ((twice quadruple) 2) ; returns 32 ((twice (twice double)) 2) ; returns 32 19 Curried Function in C Syntax (int (*)(int)) twice (int (*f)(int)) { int foo (int x) { return f(f(x)); } } return foo; (twice(double))(2); 20 Conditionals If-then-else (if (= x y) 1 2) ; in C: (x == y)? 1 : 2 Cond (cond ((= x y) 1) ((< x y) 2) (else 3)) Anything non-#f is considered true (if 1 2 3) ; returns 2 21 CSC 4101: Programming Languages 7

Equality Expression Value (eq? x x) #t (eq? x x) #t (eq? () ()) #t (eq? 2 2) undefined (eq? (list 1) (1)) #f (eqv? 2 2) #t (equal? (list 1) (1)) #t 22 Recursion (define (fac n) (if (= n 0) 1 (* n (fac (- n 1))))) (fac 5) ; returns 120 (fac 1000) ; that s 2568 digits 23 Tail-Recursion (define (fac n) (define (f n x) (if (= n 0) x (f (- n 1) (* n x)))) (f n 1)) 24 CSC 4101: Programming Languages 8

Map Function (define (map f l) (if (null? l) () (cons (f (car l)) (map f (cdr l))))) (map double (1 2 3 4 5)) ; returns (2 4 6 8 10) (map (lambda (x) (+ x 1)) (1 2)) ; returns (2 3) 25 Map with more than 2 Arguments N-ary function and N lists (map + (1 2 3) (10 20 30)) ; returns (11 22 23) 26 Let Defines local name binding (let ((x 3) (y 5)) (* x y)) Syntactic sugar for function call ((lambda (x y) (* x y)) 3 5) 27 CSC 4101: Programming Languages 9

Let* Variables are defined sequentially (let* ((x 6) (y (+ x 1))) (* x y)) 28 Assignment (but don t use it) Expression Value of x (define x 3) 3 (set! x 5) 5 (define x (list 1 2)) (1 2) (set-car! x 3) (3 2) (set-cdr! x 4) (3. 4) 29 Constants in Scheme48 Constants can t be modified Expression Value of x (define x (1 2)) (1 2) (set-car! x 3) error 30 CSC 4101: Programming Languages 10

Quicksort Problem: sort list of numbers Algorithm: If list is empty, we are done Choose pivot n (e.g., first element) Partition list into lists A and B with elements < n in A and elements > n in B Recursively sort A and B Append sorted lists and n 31 Quicksort (cont d) (define (quicksort ls) (if (null? ls) () (let ((p (car ls))) (append (quicksort (gt p (cdr ls))) (list p) (quicksort (le p (cdr ls))))))) 32 Partitioning (define (gt p ls) (filter (lambda (x) (> p x)) ls)) (define (le p ls) (filter (lambda (x) (<= p x)) ls)) 33 CSC 4101: Programming Languages 11

Filtering a List (define (filter pred ls) (cond ((null? ls) ()) ((pred (car ls)) (cons (car ls) (filter pred (cdr ls)))) (else (filter pred (cdr ls))))) 34 Trace of Quicksort (quicksort (2 5 3 1 4)) let p = 2 (gt 2 (5 3 1 4)) => (1) (quicksort (1)) let p = 1 (quicksort ()) => () (append () (1) ()) => (1) (le 2 (5 3 1 4)) => (5 3 4) (quicksort (5 3 4)) => (3 4 5) (append (1) (2) (3 4 5)) => (1 2 3 4 5) (2x) 35 CSC 4101: Programming Languages 12