A Brief Introduction to Scheme (II)

Similar documents
Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Functional Programming. Pure Functional Programming

CSc 520 Principles of Programming Languages

Building a system for symbolic differentiation

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

CS61A Midterm 2 Review (v1.1)

Building a system for symbolic differentiation

User-defined Functions. Conditional Expressions in Scheme

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

An Explicit-Continuation Metacircular Evaluator

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

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

Scheme: Strings Scheme: I/O

Programming Languages

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

A Brief Introduction to Scheme (I)

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

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

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

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

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

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

Lecture08: Scope and Lexical Address

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

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

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


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

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

Documentation for LISP in BASIC

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

6.001: Structure and Interpretation of Computer Programs

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

(scheme-1) has lambda but NOT define

YOUR NAME PLEASE: *** SOLUTIONS ***

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

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Introduction to Scheme

An Introduction to Scheme

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

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

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

CS 314 Principles of Programming Languages

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

University of Massachusetts Lowell

Scheme Quick Reference

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

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

LECTURE 16. Functional Programming

Scheme Quick Reference

Functional Programming - 2. Higher Order Functions

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages. Lecture 16

Essentials of Programming Languages Language

Essentials of Programming Languages Language

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

CSC 533: Programming Languages. Spring 2015

6.001 Notes: Section 8.1

Lecture Notes on Lisp A Brief Introduction

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

Streams and Evalutation Strategies

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

Lists in Lisp and Scheme

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Discussion 12 The MCE (solutions)

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Project 2: Scheme Interpreter

Functional Languages. Hwansoo Han

A brief tour of history

Organization of Programming Languages CS3200/5200N. Lecture 11

6.945 Adventures in Advanced Symbolic Programming

FP Foundations, Scheme

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

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

From Syntactic Sugar to the Syntactic Meth Lab:

Programming Languages

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

CS 314 Principles of Programming Languages

Typed Racket: Racket with Static Types

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

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

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

Discussion 4. Data Abstraction and Sequences

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

SOFTWARE ARCHITECTURE 6. LISP

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

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

An introduction to Scheme

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

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

Turtles All The Way Down

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

CS 360 Programming Languages Interpreters

INTERPRETATION AND SCHEME LISTS 12

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Transcription:

A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada

Lists Scheme II p.1/29

Lists Aggregate data structures are built from lists Examples: (1 2 3) A list of 3 numbers () An empty list ((1 2) (3 4)) A list of 2 lists: (1 2), (3 4) (1 (2 3) 4) A mixed list containing 3 members: 1, (2 3) and 4 (()) A singleton list containing () Scheme II p.2/29

Quoting How do we express list constants? > (1 2 3) Scheme II p.3/29

Quoting How do we express list constants? > (1 2 3) Error! Scheme II p.3/29

Quoting How do we express list constants? > (1 2 3) Error! Scheme thought you were trying to apply the procedure 1 to arguments 2 and 3. Scheme II p.3/29

Quoting How do we express list constants? > (1 2 3) Error! Scheme thought you were trying to apply the procedure 1 to arguments 2 and 3. Thus, quoting... > (quote (1 2 3)) (1 2 3) Scheme II p.3/29

Quoting How do we express list constants? > (1 2 3) Error! Scheme thought you were trying to apply the procedure 1 to arguments 2 and 3. Thus, quoting... > (quote (1 2 3)) (1 2 3) A more convenient form... > (1 2 3) (1 2 3) Scheme II p.3/29

List Constructors () evaluates to an empty list. (cons x L) Argument(s): x: any data object L: a list Return: A list with x as the first member, followed by the members of L. Scheme II p.4/29

List Constructors () evaluates to an empty list. (cons x L) Argument(s): x: any data object L: a list Return: A list with x as the first member, followed by the members of L. Examples: > (cons 1 (2 3)) (1 2 3) Scheme II p.4/29

List Constructors () evaluates to an empty list. (cons x L) Argument(s): x: any data object L: a list Return: A list with x as the first member, followed by the members of L. Examples: > (cons 1 (2 3)) (1 2 3) > (cons 1 ()) (1) Scheme II p.4/29

List Constructors () evaluates to an empty list. (cons x L) Argument(s): x: any data object L: a list Return: A list with x as the first member, followed by the members of L. Examples: > (cons 1 (2 3)) (1 2 3) > (cons 1 ()) (1) > (cons (1 2) (3 4)) ((1 2) 3 4) Scheme II p.4/29

Selector: car (car L) Argument(s): L: a non-empty list Return: the first element of L Examples: > (car (1 2 3)) 1 > (car ((1 2) 3 4)) (1 2) > (car (car ((1 2) 3 4))) 1 > (car (cons 1 (2 3))) 1 Scheme II p.5/29

Selector: cdr (cdr L) Argument(s): L: a non-empty list Return: a list containing all but the first element of L Examples: > (cdr (1 2 3)) (2 3) > (cdr ((1 2) 3 4)) (3 4) > (cdr (cdr ((1 2) 3 4))) (4) > (cdr (cons 1 (2 3))) (2 3) Scheme II p.6/29

How are Lists Represented Internally? L = (1 2 3 4) cons cell L 1 2 3 4 () car field cdr field Scheme II p.7/29

A Complex Example L L = (1 (2 3) ((4) () (5 6))) 1 () 2 3 () () () 4 () 5 6 () Scheme II p.8/29

An Alternative Notation for List Constants The previously discussed notation for list constants (i.e., (1 2 3)) is very user-friendly, but it does not make explicit the internal representation of lists. An equivalent, more explicit, but less user-friendly notation: Dotted Pairs: (car. cdr) Examples: > (2. ()) (2) > (1. (2. ())) (1 2) > ((1. ()). (2. (3. ()))) ((1) 2 3) Scheme II p.9/29

Exercise L L = (1 (2 3) ((4) () (5 6))) 1 () 2 3 () () () 4 () 5 6 () > (car (cdr L)) (2 3) > (car (car (cdr L))) 2 Exercise: How do you fetch 3, 4, 5, and 6? Scheme II p.10/29

Shorthand L L = (1 (2 3) ((4) () (5 6))) 1 () 2 3 () () () 4 () 5 6 () Shorthand for (car (cdr L)) > (cadr L) (2 3) Shorthand for (car (car (cdr L))) > (caadr L) 2 Scheme II p.11/29

Exercise You probably have guessed correctly that you can t write (caaaaaaaaaaaar L). Check out [TSPL3] Sect. 6.3 to see what combinations of car and cdr have shorthands defined. Scheme II p.12/29

Recursion with Lists (list-sum L) Argument(s): L: a list of numbers Return: the sum of all numbers in L Example: > (list-sum (2-1 4)) 5 > (list-sum ()) 0 Scheme II p.13/29

Recursion with Lists (define list-sum (lambda (L) (if (null? L) 0 (+ (car L) (list-sum (cdr L)))))) Scheme II p.14/29

Recursion with Lists (list-sum (2-1 4)) = (if (null? (2-1 4)) 0 (+ (car (2-1 4)) (list-sum (cdr (2-1 4))))) = (+ (car (2-1 4)) (list-sum (cdr (2-1 4)))) = (+ 2 (list-sum (cdr (2-1 4)))) = (+ 2 (list-sum (-1 4))) = (+ 2 (if (null? (-1 4)) 0 (+ (car (-1 4)) (list-sum (cdr (-1 4)))))) = (+ 2 (+ (car (-1 4)) (list-sum (cdr (-1 4))))) = (+ 2 (+ -1 (list-sum (4)))) Scheme II p.15/29

Recursion with Lists (list-sum (2-1 4)) =... = (+ 2 (+ -1 (list-sum (4)))) = (+ 2 (+ -1 (if (null? (4)) 0 (+ (car (4)) (list-sum (cdr (4))))))) = (+ 2 (+ -1 (+ 4 (list-sum ())))) = (+ 2 (+ -1 (+ 4 (if (null? ()) = (+ 2 (+ -1 (+ 4 0))) = 5 0 (+ (car ()) (list-sum (cdr ()))))))) Scheme II p.16/29

More Built-in Procedures Procedure (list x 1 x 2... x n ) (list? x) (null? x) (pair? x) (member x L) (length L) Meaning create a list containing the arguments test if x is a list test if x is the empty list test if x is a cons cell test if x is a member of list L the number of members of list L Scheme II p.17/29

Even More Built-in Procedures (reverse L) Returns a list containing exactly the same objects as the members of L, but in reversed order. (reverse (1 2 3)) (3 2 1) (reverse ()) () (reverse ((1 2) 3)) (3 (1 2)) (append L 1 L 2 ) Returns a list containing both the elements of L 1 and L 2, with those from L 2 following those from L 1 (append (1 2 3) (4 5)) (1 2 3 4 5) (append (1 2) ()) (1 2) (append () (1 2)) (1 2) (append ((1 2) 3) ((4))) ((1 2) 3 (4)) Scheme II p.18/29

Symbols Scheme II p.19/29

Symbols Examples: hello if a3 + zero? Symbols are case insensitive cos and COS are the same symbol Scheme II p.20/29

Quoting Revisited... Expressing symbol constant... > hello Error! Scheme thought you want to retrieve the value of the global variable hello. Again, quoting... > hello hello > (hello world) (hello world) > (a (b c) ((d))) (a (b c) ((d))) Scheme II p.21/29

Symbol-Related Procedures Procedure (symbol? x) (eq? x y) (eqv? x y) Meaning test if x is a symbol test if x & y denote the same symbol test if x & y denote the same symbol or the same number [EOPL2] uses the slower predicate eqv? to test symbol equality. Scheme II p.22/29

Meta-Programming Meta-programming: development of programs that manipulate programs Scheme programs and scheme data share the same representation symbolic lists! (lambda (x) (* x 2)) meta-programming is easy in Scheme Scheme II p.23/29

Example: Eliminating not According to [TSPL3] Sect. 5.3, the conditional form (not x) is equivalent to the following: (if x #f #t) A Scheme interpreter could choose to rewrite all expressions involving not into one without, thereby implementing the higher-level forms with a small number of core forms. Scheme II p.24/29

Example: Eliminating not (rewrite-not-exp exp) Argument(s): exp: a not expression Return: a semantically equivalent if expression Scheme II p.25/29

Example: Eliminating not (rewrite-not-exp exp) Argument(s): exp: a not expression Return: a semantically equivalent if expression Examples: > (rewrite-not-exp (not (zero? y))) (if (zero? y) #f #t) Scheme II p.25/29

Example: Eliminating not (rewrite-not-exp exp) Argument(s): exp: a not expression Return: a semantically equivalent if expression Examples: > (rewrite-not-exp (not (zero? y))) (if (zero? y) #f #t) Implementation: (define rewrite-not-exp (lambda (exp) (list if (cadr exp) #f #t) Scheme II p.25/29

Exercise The expression: (if test-expr true-expr false-expr) can be rewritten into an equivalent form: (cond (test-expr true-expr) (else false-expr)) Implement a procedure (rewrite-if-exp exp) to rewrite a given if expression exp into an equivalent cond expression. Scheme II p.26/29

Exercise Solution (define rewrite-if-exp (lambda (exp) (let ((test-expr (cadr exp) ) (true-expr (caddr exp) ) (false-expr (cadddr exp))) (list cond (list test-expr true-expr) (list else false-expr))))) Scheme II p.27/29

The let Form (let ((var 1 exp 1 ) (var 2 exp 2 )... (var n exp n )) body) 1. All expressions exp 1, exp 2,... exp n are evaluated first. 2. The results are then bound to newly created local variables var 1, var 2,..., var n. 3. With the local variables defined, evaluate body and return its result. Scheme II p.28/29

Lecture Summary This lecture concludes the overview of Scheme as presented in [TSPL3] Chap. 1 2. In this lecture... lists quoting internal representation of lists dotted pairs symbols meta-programming let Scheme II p.29/29