Today's Lecture. Programming as the process of creating a new task-specific language. data abstractions procedure abstractions higher-order procedures

Similar documents
6.001: Structure and Interpretation of Computer Programs. Themes to be integrated. Our target the art of M. C. Escher

Fall Semester, Lecture Notes { September 26, Today's lecture will use the Henderson Picture Language as an example of how we can merge

1. The Square limit language

University of Massachusetts Lowell : Organization of Programming Languages Spring 2004 Problem Set 4. Henderson Picture Language

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

6.001 Notes: Section 6.1

6.001 Notes: Section 5.1

6.001 Notes: Section 17.5

6.001 Notes: Section 31.1

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

CS450 - Structure of Higher Level Languages

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

PROFESSOR: Well, now that we've given you some power to make independent local state and to model objects,

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

Building a system for symbolic differentiation

6.001 Notes: Section 15.1

2.2 Hierarchical Data and the Closure Property

6.001 Notes: Section 8.1

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

Project 2: Scheme Interpreter

Streams and Lazy Evaluation in Lisp

Administrivia. Simple data types

Documentation for LISP in BASIC

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

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

Building a system for symbolic differentiation

Lecture #24: Programming Languages and Programs

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

Discussion 4. Data Abstraction and Sequences

Computer Graphics: Viewing in 3-D. Course Website:

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

SCHEME AND CALCULATOR 5b


A Small Interpreted Language

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

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

Problem Set 4: Streams and Lazy Evaluation

CS 314 Principles of Programming Languages

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?

Technical Arts 101 Prof. Anupam Saxena Department of Mechanical engineering Indian Institute of Technology, Kanpur. Lecture - 7 Think and Analyze

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

Principles of Programming Languages

Lexical vs. Dynamic Scope

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

Escher s Tessellations: The Symmetry of Wallpaper Patterns II. Symmetry II

Vectors in Scheme. Data Structures in Scheme

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

PROFESSOR: So far in this course we've been talking a lot about data abstraction. And remember the idea is that

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

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

Sample Final Exam Questions

Computer Graphics 7: Viewing in 3-D

An Approach to Content Creation for Trainz

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

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

CS 275 Name Final Exam Solutions December 16, 2016

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Geometry Unit 1: Transformations in the Coordinate Plane. Guided Notes

A Brief Introduction to Scheme (II)

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

Shorthand for values: variables

Adding Machine Run 2

UV Mapping to avoid texture flaws and enable proper shading

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

IGCSE ICT Section 16 Presentation Authoring

6.001: Structure and Interpretation of Computer Programs

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

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Repetition Through Recursion

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

Picking up tennis balls

Streams and Evalutation Strategies

TRANSFORMATIONS AND CONGRUENCE

Discussion 12 The MCE (solutions)

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

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

MITOCW ocw f99-lec07_300k

Guided Problem Solving

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

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

CS251 Spring 2014 Lecture 7

SMART board Training. April 2014 Erika Kindoll

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

CS2500 Exam 2 Fall 2011

On Your Own. ). Another way is to multiply the. ), and the image. Applications. Unit 3 _ _

An Explicit Continuation Evaluator for Scheme

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Introduction to Transformations. In Geometry

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

Creating a Text Frame. Create a Table and Type Text. Pointer Tool Text Tool Table Tool Word Art Tool

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

Solution Notes. COMP 151: Terms Test

TESSELLATION PROJECT DIRECTIONS

Parametric curves. Reading. Curves before computers. Mathematical curve representation. CSE 457 Winter Required:

Combining Isometries- The Symmetry Group of a Square

Higher order procedures procedures as types

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

Adobe Illustrator CS Design Professional GETTING STARTED WITH ILLUSTRATOR

Transcription:

Today's Lecture Programming as the process of creating a new task-specific language data abstractions procedure abstractions higher-order procedures

Themes to be integrated Data abstraction Separate use of data structure from details of data structure Procedural abstraction Capture common patterns of behavior and treat as black box for generating new patterns Means of combination Create complex combinations, then treat as primitives to support new combinations Use modularity of components to create new, higher level language for particular problem domain

step1: (forward 1) (no-opening-on-right?) (goto step1) (turn right 90) step2: (forward 1) (no-opening-on-left?) (goto step2) step3: (forward 1) (no-opening on left?) (goto step3) (turn left 90) (forward 1)... RM (while no-opening-on-right (forward 1)) Go to first right Go to second left Go get me coffee, please. Level of language matters. Programming is a process of inventing task-specific languages.

Our target the art of M. C. Escher ESCHER on ESCHER; Exploring the Infinite, p. 41 Harry Abrams, Inc., New York, 1989

My buddy George

A procedural definition of George (define (george) (draw-line 25 0 35 50) (draw-line 35 50 30 60) (draw-line 30 60 15 40) (draw-line 15 40 0 65) (draw-line 40 0 50 30) (draw-line 50 30 60 0) (draw-line 75 0 60 45) (draw-line 60 45 100 15) (draw-line 100 35 75 65) (draw-line 75 65 60 65) (draw-line 60 65 65 85) (draw-line 65 85 60 100) (draw-line 40 100 35 85) (draw-line 35 85 40 65) (draw-line 40 65 30 65) (draw-line 30 65 15 60) (draw-line 15 60 0 85)) 100 0 0 100 Yuck!! Where s the abstraction?

Need a data abstraction for lines (define p1 (make-vect 2 3)) (2, 3) (5, 4) (xcor p1) 2 (ycor p1) 3 (define p2 (make-vect 5 4)) (define s1 (make-segment p1 p2)) (xcor (start-segment s1)) 2 (ycor (end-segment s1)) 4

(define p1 (make-vect.25 0)) (define p2 (make-vect.35.5)) (define p3 (make-vect.3.6)) (define p4 (make-vect.15.4)) (define p5 (make-vect 0.65)) (define p6 (make-vect.4 0)) (define p7 (make-vect.5.3)) (define p8 (make-vect.6 0)) (define p9 (make-vect.75 0)) (define p10 (make-vect.6.45)) (define p11 (make-vect 1.15)) (define p12 (make-vect 1.35)) (define p13 (make-vect.75.65)) (define p14 (make-vect.6.65)) (define p15 (make-vect.65.85)) (define p16 (make-vect.6 1)) (define p17 (make-vect.4 1)) (define p18 (make-vect.35.85)) (define p19 (make-vect.4.65)) (define p20 (make-vect.3.65)) (define p21 (make-vect.15.6)) (define p22 (make-vect 0.85)) A better George (define george-lines (list (make-segment p1 p2) (make-segment p2 p3) (make-segment p3 p4) (make-segment p4 p5) (make-segment p6 p7) (make-segment p7 p8) (make-segment p9 p10) (make-segment p10 p11) (make-segment p12 p13) (make-segment p13 p14) (make-segment p14 p15) (make-segment p15 p16) (make-segment p17 p18) (make-segment p18 p19) (make-segment p19 p20) (make-segment p20 p21) (make-segment p21 p22))) Have isolated elements of abstraction Could change a point without having to redefine segments that use it Have separated data abstraction from its use

Gluing things together For pairs, use a cons: For larger structures, use a list: 1 2 3 4 (list 1 2 3 4) (cons 1 (cons 2 (cons 3 (cons 4 '() ))))

Properties of data structures Contract between constructor and selectors Property of closure: consing anything onto a list produces a list Taking the cdr of a list produces a list (except perhaps for the empty list)

Completing our abstraction Points or vectors: (define make-vect cons) (define xcor car) (define ycor cdr) Line segments: (define make-segment list) (define start-segment first) (define end-segment second)

Drawing in a rectangle or frame (1, 1) y axis origin x axis (0, 0)

Drawing lines is just algebra Drawing a line relative to a frame is just some algebra. Suppose frame has origin o, horizontal axis u and vertical axis v Then a point p, with components x and y, gets mapped to the point: o + xu + yv (1, 1) y axis origin x axis (0, 0)

Manipulating vectors o + xu + yv +vect scale-vect

(define (+vect v1 v2) (make-vect (+ (xcor v1) (xcor v2)) (+ (ycor v1) (ycor v2)))) (define (scale-vect vect factor) (make-vect (* factor (xcor vect)) (* factor (ycor vect)))) (define (-vect v1 v2) (+vect v1 (scale-vect v2 1))) (define (rotate-vect v angle) (let ((c (cos angle)) (s (sin angle))) (make-vect (- (* c (xcor v)) (* s (ycor v))) (+ (* c (ycor v)) (* s (xcor v)))))) Select parts Compute more primitive operation Reassemble new parts What is the underlying representation of a point? Of a segment?

Generating the abstraction of a frame Rectangle: (define make-rectangle list) (define origin first) (define x-axis second) (define y-axis third) y axis origin x axis Determining where to draw a point p: o + xu + yv (define (coord-map rect) p) (+vect (lambda(origin (p) rect) (+vect (+vect (origin (scale-vect rect) (x-axis rect) (xcor p)) (+vect (scale-vect(y-axis (x-axis rect) (ycor (xcor p))) )) (scale-vect (y-axis rect) (ycor p))) )))

What happens if we change how an abstraction is represented? (define make-vect list) (define xcor first) (define ycor second) Note that this still satisfies the contract for vectors What else needs to change in our system? BUPKIS, NADA, NIL, NOTHING

What is a picture? Maybe a collection of line segments? That would work for George: (define george-lines (list (make-segment p1 p2) (make-segment p2 p3)...))...but not for Mona We want to have flexibility of what we draw and how we draw it in the frame SO we make a picture be a procedure (define some-primitive-picture (lambda (rect) draw some stuff in rect )) Captures the procedural abstraction of drawing data within a frame

Creating a picture segments make-picture rect Picture proc picture on screen

The picture abstraction (define (make-picture seglist) (lambda (rect) (for-each (lambda (segment) (let* ((b (start-segment segment)) (e (end-segment segment)) (m (coord-map rect)) (b2 (m b)) (e2 (m e))) (draw-line (xcor b2) (ycor b2) (xcor e2) (ycor e2)) seglist))) Higher order procedure for-each is like map, except it doesn t collect a list of results, but simply applies procedure to each element of list for its effect let* is like let, except the names are defined in sequence, so m can be used in the expressions for b2 and e2

A better George Remember we have george-lines from before So here is George! (define george (make-picture george-lines)) (define origin1 (make-vect 0 0)) (define x-axis1 (make-vect 100 0)) (define y-axis1 (make-vect 0 100)) (define frame1 (make-rectangle origin1 x-axis1 y-axis1)) (george frame1)

Operations on pictures V rotate H O H V O

Pict ure Operations on pictures (define george (make-picture george-lines)) (george frame1) (define (rotate90 pict) (lambda (rect) (pict (make-rectangle (+vect (origin rect) (x-axis rect)) (y-axis rect) (scale-vect (x-axis rect) 1)))) (define (together pict1 pict2) (lambda (rect) (pict1 rect) (pict2 rect)))

A Georgian mess! ((together george (rotate90 george)) frame1)

Operations on pictures PictA: PictB: beside above

Creating a picture beside Picture proc rect Picture proc picture on screen

More procedures to combine pictures: (define (beside pict1 pict2 a) (lambda (rect) (pict1 (make-rectangle (origin rect) (scale-vect (x-axis rect) a) (y-axis rect))) (pict2 (make-rectangle (+vect (origin rect) (scale-vect (x-axis rect) a)) (scale-vect (x-axis rect) (- 1 a)) (y-axis rect))))) Picture operators have a closure property! (define (above pict1 pict2 a) (rotate90 ((repeated rotate90 3) (rotate90 (beside (rotate90 pict1) (rotate90 (rotate90 pict2) (beside (rotate90 a)))))) pict1) (rotate90 pict2) (define (repeated a)))))) f n) (if (= n 1) f (compose f (repeated f (- n 1)))))

Big brother (define big-brother (beside george (above empty-picture george.5).5))

A left-right flip V flip V H H (define (flip pict) (lambda (rect) (pict (make-rectangle (+vect (origin rect) (x-axis rect)) (scale-vect (x-axis rect) 1) (y-axis rect)))))

(define acrobats (beside george (rotate180 (flip george)).5)) (define rotate180 (repeated rotate90 2))

(define 4bats (above acrobats (flip acrobats).5))

Recursive combinations of pictures (define (up-push pict n) (if (= n 0) pict (above (up-push pict (- n 1)) pict.25)))

Pushing George around

Pushing George around (define (right-push pict n) (if (= n 0) pict (beside pict (right-push pict (- n 1)).75)))

Pushing George into the corner (define (corner-push pict n) (if (= n 0) pict (above (beside (up-push pict n) (corner-push pict (- n 1)).75) (beside pict (right-push pict (- n 1)).75).25)))

Pushing George into a corner (corner-push 4bats 2)

Putting copies together (define (4pict p1 r1 p2 r2 p3 r3 p4 r4) (beside (above ((repeated rotate90 r1) p1) ((repeated rotate90 r2) p2).5) (above ((repeated rotate90 r3) p3) ((repeated rotate90 r4) p4).5).5)) (define (4same p r1 r2 r3 r4) (4pict p r1 p r2 p r3 p r4)) (4same george 0 1 2 3)

(define (square-limit pict n) (4same (corner-push pict n) 1 2 0 3)) (square-limit 4bats 2)

Escher is an embedded language Scheme Scheme data Picture language Primitive data 3, #f, george nil george, mona, escher Primitive procedures +, map, rotate90, flip, Combinations (p a b) cons, car, cdr together, beside,, Abstraction Naming Creation (define ) (lambda ) (define ) (lambda ) and Scheme mechanisms (define ) (lambda )