Discussion 4. Data Abstraction and Sequences

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

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


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

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

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

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

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

6.001 recitation 3/21/07

CSE413 Midterm. Question Max Points Total 100

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

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

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

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

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

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

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

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

A Brief Introduction to Scheme (II)

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

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

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

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

Discussion 11. Streams

SCHEME AND CALCULATOR 5b

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

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

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

Midterm Examination (Sample Solutions), Cmput 325

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

Putting the fun in functional programming

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

CS450 - Structure of Higher Level Languages

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

CS 61A Midterm #2 - October 5, 1998

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

Discussion section number. a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z

Scheme as implemented by Racket

Repetition Through Recursion

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

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

Adding Machine Run 2

Module 5: Lists. Readings: HtDP, Sections 9, 10.

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

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

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

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


6.001 Notes: Section 6.1

University of Massachusetts Lowell

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

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

An Explicit-Continuation Metacircular Evaluator

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

Programming Project #4: A Logo Interpreter Summer 2005

6.001: Structure and Interpretation of Computer Programs

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

PROBLEM SOLVING 11. July 24, 2012

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

CSE 341 Section Handout #6 Cheat Sheet

CS61A Midterm 2 Review (v1.1)

Richard Feynman, Lectures on Computation

CS61A Notes Disc 11: Streams Streaming Along

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

YOUR NAME PLEASE: *** SOLUTIONS ***

April 2 to April 4, 2018

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

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

Simplifying Logical Formulas

Introduction to Scheme

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

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

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

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

6.001 Notes: Section 17.5

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees

CS 314 Principles of Programming Languages

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

How to Design Programs

CSCI 141 Computer Programming I. Filip Jagodzinski

Sample Final Exam Questions

Laboratory: Recursion Basics

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Lisp. Versions of LISP

6.001 Notes: Section 8.1

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

Lesson 1. Hello World

1. The Square limit language

12 Macros. Localmake. is an abbreviation for the two instructions. localmake "fred 87. local "fred make "fred 87

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Mutation & Data Abstraction Summer 2018 Discussion 4: July 3, Mutable Lists

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

CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees

Structure and Interpretation of Computer Programs

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

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

Transcription:

Discussion 4 Data Abstraction and Sequences Data Abstraction: The idea of data abstraction is to conceal the representation of some data and to instead reveal a standard interface that is more aligned with what the data represents as opposed to how the data is represented. To give a concrete example when you use numbers in Scheme, you don t care how they are represented you just care about whether you know what they mean. Would you prefer to code (* 22 7) to get 154 or (* 10110 111) to get 10011010 (the internal representation)? Whenever you use a certain type of data, we will usually want you to use an Abstract Data Type (ADT). What this means is you create a set of functions that define your new data type. At this point, anyone who wants to use your data type should be able to do so without having to know how you represented your data type. Let s delve into the specifics usually, you need one or more constructors, which construct a new instance of your ADT, and selectors, which allow you to retrieve relevant information from some particular instance of your ADT. We have defined the word ADT and the sentence ADT for you. The code is actually very long, because we have to deal with all of the internal representations for words and sentences. But all you need to know is that you can create a word/sentence with quote, word or sentence, and that you can select the appropriate data from words and sentences using first and butfirst. (Note: You may say that we ve also given you other procedures such as member?, every, keep and so on, but these were all built on top of the initial ADT, so really the ADT itself just consists of the constructors and selectors.) Pairs and Sequences: All you need to know about cons is that is creates a box with two compartments, into which it sticks the two arguments, and that you can get those arguments back with the selectors car and cdr. That s it. The discipline of data abstraction means that you don t need to know the internal representation. How you can think about Scheme printing a pair: It prints a (, recursively prints the car, prints., recursively prints the cdr, and prints ). Once this is done, it goes back, and if it ever sees. (, it throws away the., the (, and the matching ). What do you need to know about list? Just that (list a b c) is the same thing as (cons a (cons b (cons c ()))), and that other invocations of list behave in the same way. (We don t give a procedure definition yet because list takes a variable number

of arguments.) () is the empty list it s a special value that is a list but not a pair. You can check if something is the empty list using the predicate null?. By the way in box-and-pointer diagrams, you should always draw the starting arrow, i.e. an arrow from empty space to the pair which you are drawing. The meaning of this arrow is that the variable that I am talking about points to this pair. Exercise 1: Consider the list (1 2 3 4). Draw the box-and-pointer diagram for this list. Based on this diagram, do only the first step of how Scheme would print this list. (In other words, don t cancel out the. ( when printing the list.) (1. (2. (3. (4. ())))) Now write what Scheme actually prints. (1 2 3 4) Here s the definition of append for two arguments only (the real one can take several): (define (append x y) (if (null? x) y (cons (car x) (append (cdr x) y)))) Exercise 2: Write what Scheme prints. If it is a procedure, write procedure. If it is an error, write error and explain why, based on the definition of append given above. Also, draw a box-and-pointer diagram, if applicable. (append (1 2) 3) (1 2. 3) (append 1 (2 3)) Error The real append procedure is similar it expects all of its arguments to be lists, but it won t error in the case where only the last argument is not a list. Exercise 3: Write what Scheme prints. If it is a procedure, write procedure. If it is an error, write error. Also, draw a box-and-pointer diagram, if applicable. (cons (list 1 2) (list 3 4))

((1 2) 3 4) (append (hello world) (list (its) nice) (cons (to say) (cons (that in) ())) (scheme)) (hello world (its) nice (to say) (that in) scheme) (cons (cons (cons 1 4) 5) (cons 3 (list 4))) (((1. 4). 5) 3 4) Exercise 4: Write a procedure flatten-pairs, which given as input a list of pairs of some type of elements, produces as output a list of the elements (in the same order). For lab 5: write it twice the first time using recursion, and the second time using higher-order functions. > (flatten-pairs ((1. 2) (3. 4) (5. 6))) (1 2 3 4 5 6) > (flatten-pairs (((nested) stuff) ((is) cool))) ((nested) (stuff) (is) (cool)) (define (flatten-pairs lst) (if (null? lst) lst (cons (caar lst) (cons (cdar lst) (flatten-pairs (cdr lst)))))) (define (flatten-pairs lst) (accumulate append () (map (lambda (x) (list (car x) (cdr x))) lst))) Data Abstraction Revisited: Exercise 5: Suppose we want to write a procedure which, given a list of grades (numbers between 0 and 100), finds both the minimum and the maximum grade. Since in Scheme we can only return one thing, we ll invent a new ADT a num-pair. Assuming you have already defined the constructor, make-num-pair, and the selectors, first-num and second-num, write a procedure minmax which takes in a list of numbers and returns a num-pair whose

first-num is the minimum element and whose second-num is the maximum element. You may assume that the input list has at least one number. (Lab 5: Try doing it with both recursion and higher order functions.) (define (minmax grades) (make-num-pair (min_grades grades) (max_grades grades))) (define (min_grades grades) (if (null? (cdr grades)) (car grades) (min (car grades) (min_grades (cdr grades))))) (define (max_grades grades) (if (null? (cdr grades)) (car grades) (max (car grades) (max_grades (cdr grades))))) Instructor Note: Use this to motivate the HOF for lab 5. (define (minmax grades) (make-num-pair (accumulate min (car grades) (cdr grades)) (accumulate max (car grades) (cdr grades)))) Now let s actually write the constructors and selectors. But let s test 3 different types! Write constructors and selectors which represent a num-pair as a pair/list. > (pair? (make-num-pair 50 60)) > (second-num (minmax (89 94 83 95 91 50))) 95 (define make-num-pair cons) (define first-num car) (define second-num cdr) Write constructors and selectors which represent a num-pair as a number. (Hint: Remember that since the numbers represent grades, they must be in the range 0-100, inclusive.) > (number? (make-num-pair 50 60)) > (first-num (minmax (89 94 83 95 91 50))) 50 (define (make-num-pair a b) (+ (* 101 a) b)) (define (first-num pair) (quotient pair 101))

(define (second-num pair) (remainder pair 101)) Write constructors and selectors which represent a num-pair as a procedure. > (procedure? (make-num-pair 50 60)) > (second-num (minmax (89 94 83 95 91 50))) 95 (define (make-num-pair a b) (lambda (m) (if (= m 0) a b))) (define (first-num pair) (pair 0)) (define (second-num pair) (pair 1))