Seven constructing simple procedures using abstraction.

Slides:



Advertisements
Similar presentations
Two motion and change: programming with imperatives.
Advertisements

Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write.
Three types, subtypes, and inheritance. The story up until now Everything in your computer is data Including programs Data is divided into objects Objects.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Six compound procedures and higher-order procedures.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Twenty high-level operations on pictures. Recap: vector graphics constructors [box width height] Creates a picture with a box [group pictures …] Makes.
Eleven colors and rasters. Color objects [color name] Returns color with the specified name [color red green blue] Returns color with the specified amounts.
Ten debugging techniques. The execution process Execution proceeds in a standard series of steps Compute values of subexpressions first Then call value.
Fifteen high-level operations on pictures. Recap: vector graphics constructors [box width height] Creates a picture with a box [group pictures …] Makes.
Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, , etc. “Floating-point” numbers ,
Eight compound procedures and higher-order procedures.
Twelve painting with procedures. Overview Making shaded images with procedures Making a more elegant language Making textures with noise functions.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Thirteen conditional expressions: letting programs make “decisions”
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Six compound procedures and higher-order procedures.
Programming Epson Robots – Part 2 ME 4135 – Fall 2012 Dr. R. Lindeke.
Patterns and Sequences. Patterns refer to usual types of procedures or rules that can be followed. Patterns are useful to predict what came before or.
Games and Simulations O-O Programming in Java The Walker School
Chocolate Bar! luqili. Milestone 3 Speed 11% of final mark 7%: path quality and speed –Some cleverness required for full marks –Implement some A* techniques.
Programming Languages
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Invitation to Computer Science, Java Version, Second Edition.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Graphics and Procedures Programming Right from the Start with Visual Basic.NET 1/e 5.
Image Representation. Objectives  Bitmaps: resolution, colour depth and simple bitmap file calculations.  Vector graphics: drawing list – objects and.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
David Stotts Computer Science Department UNC Chapel Hill.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python Functions.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Digital Image Processing Lecture 12: Image Topology (Suppl)
1 Project designed and created by M. Shajith Kumar.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
JavaScript Functions. CSS Inheritance Which formatting applies? x y z input { display: block; } input.pref { background:red; } If you have a selector.
21. THE STANDARD LIBRARY. General Rules The C standard library is divided into 15 parts (24 in C99), with each part described by a header. The names of.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
String and Lists Dr. José M. Reyes Álamo.
high-level operations on pictures
EasyCode Foundations Vocabulary Terms.
Use of Java’s HashMap.
constructing simple procedures using abstraction
CS4450: Principles of Programming Languages
JavaScript Functions.
Dr. Clincy Professor of CS
Format String.
Reviewing key concepts
Coding Concepts (Basics)
String and Lists Dr. José M. Reyes Álamo.
x-Value = The horizontal value in an ordered pair or input Function = A relation that assigns exactly one value in the range to each.
Let bindings Motivation: Functions without local variables can be poor style and/or really inefficient. Syntax: let b1 b2 ... bn in e end where each bi is.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Vocabulary Memory Cards--Sample
2-Dimensional Design.
ASP control structure BRANCHING STATEMENTS
Top-Down Design with Functions
Lecture 2 - Names & Functions
Presentation transcript:

seven constructing simple procedures using abstraction

Review: rules for execution Look at the expression If it’s a number or string It’s its own value If it’s a name (i.e. a word) Look its value up in the dictionary (Check if it’s one of the special cases from the next slide) Otherwise it’s a procedure call [proc-expression arg-expression 1 … arg-expression n ] Execute all the subexpressions (proc-expression and arg-expression 1 through arg-expression n ) Run the value of, passing it the values of proc-expression, passing it the values of arg-expression 1 through arg-expression n as inputs Use its output as the value of the expression These rules are worth memorizing

Special cases If it starts with define [define name value-expression] Run value-expression Assign its value to name in the dictionary If it starts with the words with or with* [with name 1 = value-expression 1 … name last = value-expression last result-expression] Run the value-expressions Assign their values to their respective names in the dictionary Run result-expression Set the dictionary back the way it was Return the value from result-expression If it has a → inside it [name 1 … name last → result-expression] Make a procedure That names its inputs name 1 … name last And returns the value of result-expression (presumably using those names)

What is the value of this expression? [+ [× 2 2] 3]

What is the value of this expression? [+ [× 2 2] 3] 7

What is the value of this expression? [ [n → [+ n 1]] 3]

What is the value of this expression? [ [n → [+ n 1]] 3] Find the values of the subexpressions [n → [+ n 1]] is a procedure 3 is the number 3

What is the value of this expression? [ [n → [+ n 1]] 3] Find the values of the subexpressions [n → [+ n 1]] is a procdure 3 is the number 3 Okay, call the procedure with 3 as its argument Add n to the dictionary with the value 3 Execute [+ n 1] Find the values of the subexpressions + is a procedure n is 3 1 is 1 Call the procedure with 3 and 1 as arguments Output is 4 Output of procedure is 4 Value of the overall expression is 4

What is the value of this expression? [ [n → [+ n 1]] 3] 4 The rules are simple, but very subtle

Drawing a line How do we make a 300 pixel vertical line?

Drawing a line How do we make a 300 pixel vertical line? [line [point 0 0] [point 0 300]]

Drawing a line How do we make it two pixels wide? [line [point 0 0] [point 0 300]

Drawing a line How do we make it two pixels wide? [ink [pen “black” 2] [line [point 0 0] [point 0 300]]

Drawing a line How do we make it three pixels wide? [ink [pen “black” 2] [line [point 0 0] [point 0 300]]

Drawing a line How do we make it three pixels wide? [ink [pen “black” 3] [line [point 0 0] [point 0 300]]

Drawing a line How do we make a procedure that creates lines of specified widths? [ink [pen “black” 3] [line [point 0 0] [point 0 300]]

Drawing a line How do we make a procedure that creates lines of specified widths? [n → [ink [pen “black” n] [line [point 0 0] [point 0 300]]]

Drawing a line How do we make it shift the line right by n pixels? [n → [ink [pen “black” n] [line [point 0 0] [point 0 300]]]

Drawing a line How do we make it shift the line right by n pixels? [n → [ink [pen “black” n] [line [point n 0] [point n 300]]]

Drawing a line How do we make it shift the line more? [n → [ink [pen “black” n] [line [point n 0] [point n 300]]]

Drawing a line How do we make it shift the line more? [n → [ink [pen “black” n] [line [point [× 20 n] 0] [point [× 20 n] 300]]]

Theme and variation How do we get a whole series of lines of progressive widths?

Theme and variation [iterated-group [n → [ink [pen “black” n] [line [point [× n 20] 0] [point [× n 20] 300]]]] 21] generates a single line number of lines to make calls the line generator repeatedly and collects the results

Abstraction Adding variables to expressions make them less specific The expression: [line [point n 0] [point n 300]] doesn’t express any particular line It expresses something more like the abstract notion of vertical 300 pixel black lines Without specifying which vertical 300 pixel black line we mean

Programming through abstraction A good way to write simple procedures: Think of what its output should look like in some specific case 1. Write an expression for it 2. Change part of it into a variable 3. Wrap it with [variable → … ] This technique is called abstracting the expression Or, if you want to intimidate your friends, you can call it λ-abstraction 1. [line [point 0 0] [point 0 300]] 2. [line [point n 0] [point n 300]] 3. [n → [line [point n 0] [point n 300]]]

Programming through abstraction Alas, it usually isn’t that simple You often have to do something to the input before it’s really in the form you want And often times you have to play with it a bit to make it work the way you really want 1. [line [point 0 0] [point 0 300]] 2. [line [point n 0] [point n 300]] 3. [n → [line [point n 0] [point n 300]]] 4. [n → [line [point [× 20 n] 0] [point [× 20 n] 300]]]

Making a radial line pattern How do we make a pattern of lines radiating out from a central point?

Making a radial line pattern How do we make one line? [line [point 0 0] [point 0 300]]

Making a radial line pattern How do we make one rotated line? [rotate 30 [line [point 0 0] [point 0 300]]]

Making a radial line pattern How do we make an arbitrary rotated line? [rotate n [line [point 0 0] [point 0 300]]]

Making a radial line pattern How do we make a procedure that makes arbitrary rotated lines? [n → [rotate n [line [point 0 0] [point 0 300]]]]

Making a radial line pattern How do we make a lot of rotated lines? [iterated-group [n → [rotate n [line [point 0 0] [point 0 300]]]] 10]

Fixing it up How do we make a full circle? [iterated-group [n → [rotate [× 36 n] [line [point 0 0] [point 0 300]]]] 10]