User-defined Functions. Conditional Expressions in Scheme

Similar documents
Functional Programming - 2. Higher Order Functions

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. Pure Functional Programming

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Functional Programming. Pure Functional 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.

Functional Programming. Pure Functional Languages

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

FP Foundations, Scheme

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

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages. Lecture 16

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

Vectors in Scheme. Data Structures in Scheme

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

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

Functional Programming. Big Picture. Design of Programming Languages

CS 314 Principles of Programming Languages

Scheme: Strings Scheme: I/O

Scheme Quick Reference

Organization of Programming Languages CS3200/5200N. Lecture 11

Project 2: Scheme Interpreter

Functional programming with Common Lisp

CS 314 Principles of Programming Languages

Scheme Quick Reference

6.001 Notes: Section 8.1

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Building a system for symbolic differentiation

Chapter 15 Functional Programming Languages

Lisp Basic Example Test Questions

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

LECTURE 16. Functional Programming

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

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

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

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

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

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

Programming Languages

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

CS61A Midterm 2 Review (v1.1)

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

ALISP interpreter in Awk

Introduction to Scheme

Documentation for LISP in BASIC

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

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

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?

YOUR NAME PLEASE: *** SOLUTIONS ***

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

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

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

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

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

Functional Programming. Contents

Comp 311: Sample Midterm Examination

Chapter 15. Functional Programming Languages

Introductory Scheme. Revision 1

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

Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

Building a system for symbolic differentiation

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

CSE 341 Section Handout #6 Cheat Sheet

Typed Scheme: Scheme with Static Types

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

Fall 2017 December 4, Scheme. Instructions

Principles of Programming Languages

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

Scheme: Expressions & Procedures

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Typed Racket: Racket with Static Types

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

Functional Programming

CSC 533: Programming Languages. Spring 2015

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

SCHEME AND CALCULATOR 5b

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Administrivia. Simple data types

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

Repetition Through Recursion

Lambda Calculus see notes on Lambda Calculus

Pattern Matching for Scheme

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

Lisp. Versions of LISP

Evaluating Scheme Expressions

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


CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

An Explicit Continuation Evaluator for Scheme

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

What is tail position?

Fall 2017 December 4, 2017

An introduction to Scheme

Transcription:

User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment function. Similarly, ((lambda (x (+ x 1 10 11 We can bind values and functions to global symbols using the define function. The general form is id object id is not d but object is. id is bound to the value object s to. pi 3.1415926535 plus1 (lambda (x (+ x 1 pi*2 (* pi 2 Once a symbol is defined, it s to the value it is bound to: (plus1 12 13 111 112 Since functions are frequently defined, we may abbreviate id (lambda (args (body as (id args (body (plus1 x (+ x 1 Conditional Expressions in Scheme A predicate is a function that returns a boolean value. By convention, in Scheme, predicate names end with? number? symbol? equal? null? list? In conditionals, #f is false, and everything else, including #t, is true. The if expression is (if pred E1 E2 First pred is d. Depending on its value (#f or not, either E1 or E2 is d (but not both and returned as the value of the if expression. 113 114

(if (= 1 (+ 0 1 'Yes 'No (fact n (if (= n 0 1 (* n (fact (- n 1 Generalized Conditional This is similar to a switch or case: (cond (p1 e1 (p2 e2... (else en Each of the predicates (p1, p2,... is d until one is true ( #f. Then the corresponding expression (e1, e2,... is d and returned as the value of the cond. else acts like a predicate that is always true. Example: (cond ((= a 1 2 ((= a 2 3 (else 4 115 116 Recursion in Scheme Recursion is widely used in Scheme and most other functional programming languages. Rather than using a loop to step through the elements of a list or array, recursion breaks a problem on a large data structure into a simpler problem on a smaller data structure. A good example of this approach is the append function, which joins (or appends two lists into one larger list containing all the elements of the two input lists (in the correct order. Note that cons is not append. cons adds one element to the head of an existing list. (cons '(a b '(c d ((a b c d (append '(a b '(c d (a b c d The append function is predefined in Scheme, as are many other useful list-manipulating functions (consult the Scheme definition for what s available. It is instructive to define append directly to see its recursive approach: (append L1 L2 (if (null? L1 L2 (cons (car L1 (append (cdr L1 L2 117 118

Let s trace (append '(a b '(c d Our definition is (append L1 L2 (if (null? L1 L2 (cons (car L1 (append (cdr L1 L2 Now L1 = (a b and L2 = (c d. (null? L1 is false, so we (cons (car L1 (append (cdr L1 L2 = (cons (car '(a b (append (cdr '(a b '(c d = (cons 'a (append '(b '(c d We need to (append '(b '(c d In this call, L1 = (b and L2 = (c d. L1 is not null, so we (cons (car L1 (append (cdr L1 L2 = (cons (car '(b (append (cdr '(b '(c d = (cons 'b (append '( '(c d We need to (append '( '(c d In this call, L1 = ( and L2 = (c d. L1 is null, so we return (c d. Therefore (cons 'b (append '( '(c d = (cons 'b '(c d = (b c d = (append '(b '(c d Finally, (append '(a b '(c d = (cons 'a (append '(b '(c d = (cons 'a '(b c d = (a b c d Note: Source files for append, and other Scheme examples, are in ~cs538-1/public/scheme/example1.scm, ~cs538-1/public/scheme/example2.scm, etc. 119 120 Reversing a List Another useful list-manipulation function is rev, which reverses the members of a list. That is, the last element becomes the first element, the next-to-last element becomes the second element, etc. (rev '(1 2 3 (3 2 1 The definition of rev is straightforward: (rev L (if (null? L L (append (rev (cdr L (list (car L As an example, consider (rev '(1 2 Here L = (1 2. L is not null so we (append (rev (cdr L (list (car L = (append (rev (cdr '(1 2 (list (car '(1 2 = (append (rev '(2 (list 1 = (append (rev '(2 '(1 We must (rev '(2 Here L = (2. L is not null so we (append (rev (cdr L (list (car L = (append (rev (cdr '(2 (list (car '(2 = (append (rev ((list 2 = (append (rev ('(2 We must (rev '( Here L = (. L is null so (rev '(= ( 121 122

(append (rev ('(2 = (append ( '(2 = (2 = (rev '(2 Finally, recall (rev '(1 2 = (append (rev '(2 '(1 = (append '(2 '(1 = (2 1 As constructed, rev only reverses the top level elements of a list. That is, members of a list that themselves are lists aren t reversed. (rev '( (1 2 (3 4 = ((3 4 (1 2 We can generalize rev to also reverse list members that happen to be lists. To do this, it will be convenient to use Scheme s let construct. The Let Construct Scheme allows us to create local names, bound to values, for use in an expression. The structure is (let ( (id1 val1 (id2 val2... expr In this construct, val1 is d and bound to id1, which will exist only within this let expression. If id1 is already defined (as a global or parameter name the existing definition is hidden and the local definition, bound to val1, is used. Then val2 is d and bound to id2,... Finally, expr is d in a scope that includes id1, id2,... 123 124 (let ( (a 10 (b 20 (+ a b 30 Using a let, the definition of revall, a version of rev that reverses all levels of a list, is easy: (revall L (if (null? L L (let ((E (if (list? (car L (revall (car L (car L (append (revall (cdr L (list E (revall '( (1 2 (3 4 ((4 3 (2 1 Subsets Another good example of Scheme s recursive style of programming is subset computation. Given a list of distinct atoms, we want to compute a list of all subsets of the list values. (subsets '(1 2 3 ( ( (1 (2 (3 (1 2 (1 3 (2 3 (1 2 3 The order of atoms and sublists is unimportant, but all possible subsets of the list values must be included. Given Scheme s recursive style of programming, we need a recursive definition of subsets. 125 126

That is, if we have a list of all subsets of n atoms, how do we extend this list to one containing all subsets of n+1 values? First, we note that the number of subsets of n+1 values is exactly twice the number of subsets of n values. (subsets '(1 2 ( ( (1 (2 (1 2, which contains 4 subsets. (subsets '(1 2 3 contains 8 subsets (as we saw earlier. Moreover, the extended list (of subsets for n+1 values is simply the list of subsets for n values plus the result of distributing the new value into each of the original subsets. (subsets '(1 2 3 ( ( (1 (2 (3 (1 2 (1 3 (2 3 (1 2 3 = ( ( (1 (2 (1 2 plus ( (3 (1 3 (2 3 (1 2 3 This insight leads to a concise program for subsets. We will let (distrib L E be a function that distributes E into each list in L. (distrib '(( (1 (2 (1 2 3 = ( (3 (3 1 (3 2 (3 1 2 (distrib L E (if (null? L ( (cons (cons E (car L (distrib (cdr L E 127 128 We will let (extend L E extend a list L by distributing element E through L and then appending this result to L. (extend '( ( (a 'b ( ( (a (b (b a (extend L E (append L (distrib L E Now subsets is easy: (subsets L (if (null? L (list ( (extend (subsets (cdr L (car L 129