CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

Similar documents
CS3: Introduction to Symbolic Programming. Lecture 14: Lists Scheme vs. other programming languages.

CS3: Introduction to Symbolic Programming. Lecture 14: Lists Scheme vs. other programming languages.

CS3: Introduction to Symbolic Programming. Lecture 11: Tree Recursion, beginning lists, and Midterm 2. Spring 2007 Nate Titterton

CS3 Midterm 2 Standards and Solutions

CS3: Introduction to Symbolic Programming. Lecture 5:

YOUR NAME PLEASE: *** SOLUTIONS ***


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

CS3: Introduction to Symbolic Programming. Lecture 10: Tic-tac-toe Lambda.

Discussion 4. Data Abstraction and Sequences

CS61A Midterm 2 Review (v1.1)

CS 61A Midterm #2 - October 5, 1998

CS3: Introduction to Symbolic Programming. Lecture 8: Introduction to Higher Order Functions. Spring 2008 Nate Titterton

Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

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

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

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

CS 314 Principles of Programming Languages

Sample Final Exam Questions

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

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

A Brief Introduction to Scheme (II)

Read and fill in this page now

Defining Recursive Procedures. Lecture 8: Recursing Lists. list? Tracing list? sumlist. Defining Recursive Procedures on Lists

Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below.

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

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

CS 314 Principles of Programming Languages. Lecture 16

Warm-up! CS 3 Final Review. Predict the output. Predict the Output. Predict the output. Predict the output. What is your favorite color?

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

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

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

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

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

CS61A Notes Disc 11: Streams Streaming Along

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

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

CS3 Midterm 2 Summer 2008

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

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))

Functional Programming. Pure Functional Programming

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

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))

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Read and fill in this page now. Your instructional login (e.g., cs3-ab): Your lab section days and time: Name of the person sitting to your left:

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

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

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

University of Massachusetts Lowell

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Functional Programming - 2. Higher Order Functions

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

Fall 2017 December 4, 2017

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

Department of Electrical Engineering and Computer Sciences Spring 2001 Instructor: Dan Garcia CS 3 Midterm #2. Personal Information

CS450 - Structure of Higher Level Languages

Scheme: Strings Scheme: I/O

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

CS 61B, Spring 1999 MT3 Professor M. Clancy

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

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

User-defined Functions. Conditional Expressions in Scheme

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

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

CS 275 Name Final Exam Solutions December 16, 2016

Fall 2017 December 4, Scheme. Instructions

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

An Explicit-Continuation Metacircular Evaluator

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

CS 415 Midterm Exam Fall 2003

Discussion 11. Streams

Putting the fun in functional programming

Introduction to Scheme

Building a system for symbolic differentiation

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

CS 314 Principles of Programming Languages

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CSC 533: Programming Languages. Spring 2015

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

Lisp. Versions of LISP

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CS3 Midterm 1 Fall 2006

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

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

Programming Languages

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Functional Programming. Pure Functional Languages

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

CSE 341 Section Handout #6 Cheat Sheet

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

Administrivia. Simple data types

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

Structure and Interpretation of Computer Programs

Quiz 1. Queues , Fall 2007 Recitation 10 Solutions 1

CSc 520 Principles of Programming Languages

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

Scheme: Expressions & Procedures

Building a system for symbolic differentiation

Transcription:

CS3: Introduction to Symbolic Programming Lecture 14: Lists Fall 2006 Nate Titterton nate@berkeley.edu

Schedule 13 14 15 16 April 16-20 April 23-27 Apr 30-May 4 May 7 Thursday, May 17 Lecture: CS3 Projects, Lists Lab: Begin work on CS3 Big Project Reading: Simply Scheme, chapter 20 Lecture: Non-functional programming, lists, project review Lab: Non-functional programming Work on projects Lecture: CS at Berkeley (guest lecture) Lab: Finish projects (due end of week) Lecture: Exam review no more labs! Final Exam, 5-8pm F295 Haas

Midterm #2 Any questions? 4) tail-recursive roman-sum 5) price-is-right 6) last-letter 7) chips, drinks, and gum -- snack3

Project Check-offs There are 3 checkoffs You need to do them on time in order to get credit for the project 3. Tell your TA which project you will do and who you will do it with 4. Show your TA that you have accomplished something. S/he will comment. 5. Show that you have most of the work done: your TA will run your code.

Lists

Lists: review of new procedures Constructors -append -list -cons Selectors -car -cdr HOF -map -filter -reduce -apply

What goes in a list? Answer: anything! So, (word? x) (not (list? x)) are not the same thing!

A few other important topics re: lists 2. map can take multiple arguments 4. apply 6. Association lists 8. Generalized lists

map can take multiple list arguments (map + '(1 2 3) '(100 200 300)) (101 202 303) The argument lists have to be the same length (define (palindrome? lst) (all-true? (map equal? lst (reverse lst)))) (palindrome? '(a m a n a p l a n a c a n a l p a n a m a)) #t Write all-true?, without using cond/if.

apply (not the same as accumulate!) apply takes a function and a list, and calls the function with the elements of the list as its arguments: (apply + '(1 2 3)) (apply cons '(joe (bob))) (apply day-span '((january 1) (december 31)))

Association lists Used to associate key-value pairs ((i 1) (v 5) (x 10) (l 50) (c 100) (d 500) (m 1000)) assoc looks up a key and returns a pair (assoc 'c '((i 1) (v 5) (x 10) ) ) (c 100) ;; Write sale-price, which takes a list of items ;; and returns a total price (define *price-list* '((bread 2.89) (milk 2.33) (cheese 5.21) (chocolate.50) (beer 6.99) (tofu 1.67) (pasta.69))) (sale-price '(bread tofu))

Generalized lists Elements of a list can be anything, including any list Lab materials discuss -flatten (3 ways) -completely-reverse - processing a tree-structured directory

How about this flatten? (define (flatten thing) (if (list? thing) (reduce (map flatten thing)) ( thing)))

Write deep-member? (deep-member? 'b '((a b) (c d) (e f) (g h i)) ) #t (deep-member? 'x '((a b) (c d) (e f) (g h i)) ) #f (deep-member? '(c d) '((a b) (c d) (e f) (g h i)) ) #t

CS3: Introduction to Symbolic Programming Lecture 14: Lists Fall 2006 Nate Titterton nate@berkeley.edu

Schedule 13 14 15 16 April 16-20 April 23-27 Apr 30-May 4 May 7 Thursday, May 17 Lecture: CS3 Projects, Lists Lab: Begin work on CS3 Big Project Reading: Simply Scheme, chapter 20 Lecture: Non-functional programming, lists, project review Lab: Non-functional programming Work on projects Lecture: CS at Berkeley (guest lecture) Lab: Finish projects (due end of week) Lecture: Exam review no more labs! Final Exam, 5-8pm F295 Haas Spring 2006 CS3: 2 2

Midterm #2 Any questions? 4) tail-recursive roman-sum 5) price-is-right 6) last-letter 7) chips, drinks, and gum -- snack3 Spring 2006 CS3: 3

Project Check-offs There are 3 checkoffs You need to do them on time in order to get credit for the project 3. Tell your TA which project you will do and who you will do it with 4. Show your TA that you have accomplished something. S/he will comment. 5. Show that you have most of the work done: your TA will run your code. Spring 2006 CS3: 4

Lists

Lists: review of new procedures Constructors -append -list -cons Selectors -car -cdr HOF -map -filter -reduce -apply Spring 2006 CS3: 6

What goes in a list? Answer: anything! So, (word? x) (not (list? x)) are not the same thing! Spring 2006 CS3: 7 See the slide on flatten, and compare the code on the slide to the code on ucwise: in the slide, we use the proper "(not (list? thing))" rather than "(word? thing)", which won't be fooled by booleans and procedures (i.e., things that aren't words but aren't lists either). 7

A few other important topics re: lists 2. map can take multiple arguments 4. apply 6. Association lists 8. Generalized lists Spring 2006 CS3: 8

map can take multiple list arguments (map + '(1 2 3) '(100 200 300)) (101 202 303) The argument lists have to be the same length (define (palindrome? lst) (all-true? (map equal? lst (reverse lst)))) (palindrome? '(a m a n a p l a n a c a n a l p a n a m a)) #t Write all-true?, without using cond/if. Spring 2006 CS3: 9 (define (all-true? lst) (or (null? lst) (and (car lst) (all-true? (cdr lst))))) 9

apply (not the same as accumulate!) apply takes a function and a list, and calls the function with the elements of the list as its arguments: (apply + '(1 2 3)) (apply cons '(joe (bob))) (apply day-span '((january 1) (december 31))) Spring 2006 CS3: 10

Association lists Used to associate key-value pairs ((i 1) (v 5) (x 10) (l 50) (c 100) (d 500) (m 1000)) assoc looks up a key and returns a pair (assoc 'c '((i 1) (v 5) (x 10) ) ) (c 100) ;; Write sale-price, which takes a list of items ;; and returns a total price (define *price-list* '((bread 2.89) (milk 2.33) (cheese 5.21) (chocolate.50) (beer 6.99) (tofu 1.67) (pasta.69))) (sale-price '(bread tofu)) Spring 2006 CS3: 11 (define *price-list* '((bread 2.89) (milk 2.33) (cheese 5.21) (chocolate.50) (beer 6.99) (tofu 1.67) (pasta.69))) (define (sale-price items)) (* 1.0825 ;; tax, why not (apply + (map (lambda (i) (cadr (assoc i *price-list*))) items)))) # (sale-price '(cheese milk pasta tofu) *price-list*) ;; 10.71675 (sale-price '(beer beer beer beer) *price-list*) ;; 30.2667 # 11

Generalized lists Elements of a list can be anything, including any list Lab materials discuss -flatten (3 ways) -completely-reverse - processing a tree-structured directory Spring 2006 CS3: 12

How about this flatten? (define (flatten thing) (if (list? thing) (reduce (map flatten thing)) ( thing))) Spring 2006 CS3: 13 ;; The way to think about this is to "trust ;; the recursion". "flatten" has to return a flat list, right? So, both ;; cases in the if have to return properly flattened lists. ;; what is (map flatten thing) going to return? ;; well, it has to be something like this: ;; ( (a b c) (d e f) (g h i) ) ;; or, a "list of flat lists". The full reduce has to return, when given ;; this, ;; ( a b c d e f g h i ) ;; or a properly flat list. With that, you should be able to fill ;; in the first blank. ;; The second blank is also easy, when you realize that the return value ;; must be a flat list. "thing" is a word (or, more properly, not a list). ;; So, turning it into a flat list is easy! ;; Here is the solution (define (flatten thing) (if (list? thing) (reduce append (map flatten thing)) (list thing))) 13

Write deep-member? (deep-member? 'b '((a b) (c d) (e f) (g h i)) ) #t (deep-member? 'x '((a b) (c d) (e f) (g h i)) ) #f (deep-member? '(c d) '((a b) (c d) (e f) (g h i)) ) #t Spring 2006 CS3: 14 ;; similar to solution for flatten (define (deep-member? item gl) (cond ((null? gl) #f) ((list? (car gl)) (or (equal? item (car gl)) (deep-member? item (car gl)) (deep-member? item (cdr gl)) ) ) (else ;; first element is a non-list (or (equal? item (car gl)) (deep-member? item (cdr gl))) ))) ;; another way (define (deep-member? item gl) (cond ((null? gl) #f) ((equal? item (car gl)) #t) ; checks with either a list or nonlist as first element ((list? (car gl)) (or (deep-member? item (car gl)) (deep-member? item (cdr gl)) ) ) (else (deep-member? item (cdr gl))) )) 14