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

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

CSc 520 Principles of Programming Languages

A Brief Introduction to Scheme (II)

Functional Programming. Pure Functional Languages

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

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

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

Project 2: Scheme Interpreter

Lists in Lisp and Scheme

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

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

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

Building a system for symbolic differentiation

CSc 520 Principles of Programming Languages

Introduction to Scheme

Essentials of Programming Languages Language

Scheme Quick Reference

CS 314 Principles of Programming Languages. Lecture 16

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

Scheme Quick Reference

Scheme: Strings Scheme: I/O

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Essentials of Programming Languages Language

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

CS61A Midterm 2 Review (v1.1)

CSC 533: Programming Languages. Spring 2015

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

CS 314 Principles of Programming Languages

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

Building a system for symbolic differentiation

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

Functional Programming. Pure Functional Programming

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

Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

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

Lecture Notes on Lisp A Brief Introduction

An Explicit-Continuation Metacircular Evaluator

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

Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Documentation for LISP in BASIC

COP4020 Programming Assignment 1 - Spring 2011

CSE413 Midterm. Question Max Points Total 100

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Problem 1: Binary Trees: Flatten

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

LECTURE 16. Functional Programming

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

SOFTWARE ARCHITECTURE 6. LISP

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

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

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

Lisp. Versions of LISP

CSc 520 Principles of Programming Languages

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

How to Design Programs 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?

Simplifying Logical Formulas

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

User-defined Functions. Conditional Expressions in Scheme

How to Design Programs Languages

Recursion & Iteration

University of Massachusetts Lowell

CSc 520. Principles of Programming Languages 11: Haskell Basics

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CSc 520. Gofer III. Accumulative Recursion. Accumulative Recursion... Stack Recursion. Principles of Programming Languages. Christian Collberg

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

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

Representing Images as Lists of Spots

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

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 - 2. Higher Order Functions

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

CS 275 Name Final Exam Solutions December 16, 2016

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

CS 314 Principles of Programming Languages

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

Imperative, OO and Functional Languages A C program is

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

Functional programming with Common Lisp

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

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Scheme as implemented by Racket

6.945 Adventures in Advanced Symbolic Programming

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

Functional Programming. Big Picture. Design of Programming Languages

Turtles All The Way Down

How to Design Programs

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

Vectors in Scheme. Data Structures in Scheme

Functional Programming

CSE 341 Section Handout #6 Cheat Sheet

Lexical vs. Dynamic Scope

(scheme-1) has lambda but NOT define

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

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

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Transcription:

CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg April 22, 2005 1 Constructing Lists The most important data structure in Scheme is the list. Lists are constructed using the function cons: (cons first rest cons returns a list where the first element is first, followed by the elements from the list rest. 2 Constructing Lists... > (cons a ( (a > (cons a (cons b ( (a b > (cons a (cons b (cons c ( (a b c There are a variety of short-hands for constructing lists. Lists are heterogeneous, they can contain elements of different types, including other lists. > (a b c (a b c > (list a b c (a b c > (1 a "hello" (1 a "hello" 1

3 Examining Lists (car L returns the first element of a list. Some implementations also define this as (first L. (cdr L returns the list L, without the first element. Some implementations also define this as (rest L. Note that car and cdr do not destroy the list, just return its parts. 4 Examining Lists... Note that (cdr L always returns a list. 5 Examining Lists... > (car (a b c a > (cdr (a b c (b c > (car (cdr (a b c b > (cdr (a b c (b c > (cdr (cdr (a b c (c > (cdr (cdr (cdr (a b c ( > (cdr (cdr (cdr (cdr (a b c error A shorthand has been developed for looking deep into a list: Each "a" stands for a car, each "d" for a cdr. For example, (caddar L stands for (clist of "a" and "d"r L (car (cdr (cdr (car L > (cadr (a b c b > (cddr (a b c (c > (caddr (a b c c 2

6 Lists of Lists Any S-expression is a valid list in Scheme. That is, lists can contain lists, which can contain lists, which... 7 List Equivalence > (a (b c (a (b c > (1 "hello" ("bye" 1/4 (apple (1 "hello" ("bye" 1/4 (apple > (caaddr (1 "hello" ("bye" 1/4 (apple "bye" (equal? (eqv? L1 L2 does a structural comparison of two lists, returning if they look the same. L1 L2 does a pointer comparison, returning if two lists are the same object. 8 List Equivalence... > (eqv? (a b c (a b c false > (equal? (a b c (a b c This is sometimes referred to as deep equivalence vs. shallow equivalence. 9 Predicates on Lists > (define mylist (a b c > (eqv? mylist mylist > (eqv? (a (b c (d (a (b c (d false > (equal? (a (b c (d (a (b c (d (null? (list? L returns for an empty list. L returns if the argument is a list. > (null? ( > (null? (a b c #f > (list? (a b c > (list? "(a b c" #f 3

10 List Functions Examples... > (memq z (x y z w > (car (cdr (car ((a b (c d (c d > (caddr ((a b (c d (c d > (cons a ( (a > (cons d (e (d e > (cons (a b (c d ((a b (c d 11 Recursion over Lists cdr-recursion Compute the length of a list. This is called cdr-recursion. (define (length x [(null? x 0] [else (+ 1 (length (cdr x] > (length (1 2 3 3 > (length (a (b c (d e f 3 12 Recursion over Lists car-cdr-recursion Count the number of atoms in an S-expression. This is called car-cdr-recursion. (define (atomcount x [(null? x 0] [(list? x (+ (atomcount (car x (atomcount (cdr x] [else 1] > (atomcount (1 1 > (atomcount ("hello" a b (c 1 (d 6 4

13 Recursion Over Lists Returning a List Map a list of numbers to a new list of their absolute values. In the previous examples we returned an atom here we re mapping a list to a new list. (define (abs-list L [(null? L (] [else (cons (abs (car L (abs-list (cdr L] > (abs-list (1-1 2-3 5 (1 1 2 3 5 14 Recursion Over Two Lists (atom-list-eq? L1 L2 returns if L1 and L2 are the same list of atoms. (define (atom-list-eq? L1 L2 [(and (null? L1 (null? L2 ] [(or (null? L1 (null? L2 #f] [else (and (atom? (car L1 (atom? (car L2 (eqv? (car L1 (car L2 (atom-list-eq? (cdr L1 (cdr L2] 15 Recursion Over Two Lists... > (atom-list-eq? (1 2 3 (1 2 3 > (atom-list-eq? (1 2 3 (1 2 a #f 5

16 Append (define (append L1 L2 [(null? L1 L2] [else (cons (car L1 (append (cdr L1 L2] > (append (1 2 (3 4 (1 2 3 4 > (append ( (3 4 (3 4 > (append (1 2 ( (1 2 17 Deep Recursion equal? (define (equal? x y (or (and (atom? x (atom? y (eq? x y (and (not (atom? x (not (atom? y (equal? (car x (car y (equal? (cdr x (cdr y > (equal? a a > (equal? (a (a > (equal? ((a ((a 18 Patterns of Recursion cdr-recursion We process the elements of the list one at a time. Nested lists are not descended into. (define (fun L [(null? L return-value] [else...(car L...(fun (cdr L...] 19 Patterns of Recursion car-cdr-recursion We descend into nested lists, processing every atom. 6

(define (fun x [(null? x return-value] [(atom? x return-value] [(list? x...(fun (car x......(fun (cdr x...] [else return-value] 20 Patterns of Recursion Maps Here we map one list to another. 21 Example: Binary Trees (define (map L [(null? L (] [else (cons (...(car L... (map (cdr L] A binary tree can be represented as nested lists: Each node is represented by a triple Empty subtrees are represented by (. 22 Example: Binary Trees... (4 (2 ( ( ( 6 ( 5 ( ( ( (data left-subtree right-subtree (define (key tree (car tree (define (left tree (cadr tree (define (right tree (caddr tree (define (print-spaces N [(= N 0 ""] [else (begin (display " " (print-spaces (- N 1 (define (print-tree tree (print-tree-rec tree 0 7

23 Example: Binary Trees... (define (print-tree-rec tree D [(null? tree] [else (begin (print-spaces D (display (key tree (newline (print-tree-rec (left tree (+ D 1 (print-tree-rec (right tree (+ D 1 ] > (print-tree (4 (2 ( ( (6 (5 ( ( ( 4 2 6 5 24 Binary Trees using Structures We can use structures to define tree nodes. (define-struct node (data left right (define (tree-member x T [(null? T #f] [(= x (node-data T ] [(< x (node-data T (tree-member x (node-left T] [else (tree-member x (node-right T] 25 Binary Trees using Structures... (define tree (make-node 4 (make-node 2 ( ( (make-node 6 (make-node 5 ( ( (make-node 9 ( ( > (tree-member 4 tree > (tree-member 5 tree > (tree-member 19 tree false 8

26 Homework Write a function swapfirsttwo which swaps the first two elements of a list. Example: (1 2 3 4 (2 1 3 4. Write a function swaptwoinlists which, given a list of lists, forms a new list of all elements in all lists, with first two of each swapped. Example: ((1 2 3 (4 (5 6 (2 1 3 4 6 5. 9