Six compound procedures and higher-order procedures.

Slides:



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

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Programming Languages and Paradigms Lisp Programming.
Three types, subtypes, and inheritance. The story up until now Everything in your computer is data Including programs Data is divided into objects Objects.
Comparing Integers Lesson
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array.
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.
Sixteen lists and compound data. Recap Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define.
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.
Seven constructing simple procedures using abstraction.
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.
Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after)  Everyone with a partner that wants a partner?  Extra Office.
Thirteen conditional expressions: letting programs make “decisions”
Chapter 3.5 Logic Circuits. How does Boolean algebra relate to computer circuits? Data is stored and manipulated in a computer as a binary number. Individual.
Ch 1.3 – Order of Operations
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
MA 1128: Lecture 17 – 6/17/15 Adding Radicals Radical Equations.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
9.4 Special Cases.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
Equations Inequalities = > 3 5(8) - 4 Numerical
Chapter 1 Review - Get a whiteboard and marker per pair - Take out a blank sheet of paper.
LOGIC CIRCUITLOGIC CIRCUIT. Goal To understand how digital a computer can work, at the lowest level. To understand what is possible and the limitations.
Operational Semantics of Scheme
Lecture #5 מבוא מורחב.
Morgan Kaufmann Publishers
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Computer Science 210 Computer Organization
Chapter 4 Operations on Bits.
high-level operations on pictures
Class 11: Two-argument recursion
constructing simple procedures using abstraction
Logic Gates.
Logic Gates Benchmark Companies Inc PO Box Aurora CO
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Operators and Expressions
Higher-Order Procedures
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Chapter 4: Types.
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
Functional Programming
Bellwork: Monday (-4) = Today we will do a Number Talk.
The Metacircular Evaluator
Logic Gates.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
GCSE Computer Science – Logic Gates & Boolean Expressions
Lecture 12: Message passing The Environment Model
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.
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
Rules of evaluation The value of a number is itself.
Good programming practices
Programming Techniques :: Arithmetic & Boolean Operators
Lecture 2 - Names & Functions
Presentation transcript:

six compound procedures and higher-order procedures

A box figure ► [group [box ] [translate [point ] [box 70 70]] [translate [point 100 −100] [box 70 70]] [translate [point −100 −100] [box 70 70]] [translate [point − ] [box 70 70]]]

Simplifying with names ► [define frame [box ]] ► [define little [box 70 70]] ► [group frame [translate [point ] little] [translate [point 100 −100] little] [translate [point −100 −100] little] [translate [point − ] little]]

What’s wrong with this? Defining little and frame saves us a little bit of work, but … What we really want to do is to name the whole pattern of making shifted boxes… We need abstraction

What we want to be able to say ► [define frame [box ]] ► [group frame [shifted ] [shifted 100 −100] [shifted −100 −100] [shifted − ]]

What kind of a thing is shifted? Well, it takes inputs It returns an output It must be a procedure… In fact, we even know what it should do It should make a point from its two arguments Make a box Translate the box by the point And return it

Compound procedures [arg 1 arg 2 … arg n → exp] Procedures are just another data object You can construct new procedures from old using the → operator When called, the procedure Sets the local names arg 1 arg 2 … arg n to the arguments passed to the procedure Computes the value of exp using the values of the arguments Returns the value of exp Note: you type the → symbol by first typing – and then >

Defining shifted ► [define shifted [x y → [translate [point x y] little]]] ► [group frame [shifted ] [shifted 100 −100] [shifted −100 −100] [shifted − ]]

A boring example Meta includes procedures for the standard arithmetic operations like +, -, ×, / (division), etc. You type the × symbol by typing the * key Square is a compound procedure built from × Polynomial is a compound procedure built from ×, +, and square Notice they both use the name n for their argument But it’s okay because they’re local names ► [define square [n → [× n n]]] ► [square 2] 4 ► [square 58.7] ► [define polynomial [n → [+ [square n] [× n 2] 4]]] ► [polynomial 32] 1092 ►

A more interesting procedure We haven’t taught you enough at this point to understand how iterated-group works You’ll understand in a few weeks For the moment, just copy it from this slide and use it But the thing we do want you to understand is that You give it a procedure that makes pictures And a number of times to call it And it gives you a group of all the results of calling the procedure with the arguments 0, 1, 2, etc. ► [define iterated-group [proc count → [apply group [up-to count proc]]]]

Using iterated-group [iterated-group [n → [line [point [× n 20] 0] [point [× n 20] 300]]] 10]

Using iterated-group [iterated-group [n → [ink [pen 'black n] [line [point [× n 20] 0] [point [× n 20] 300]]]] 21]

Using iterated-group ► [iterated-group [n → [translate [point [× n 20] [× n 20]] [box 10 10]]] 5] ►

Using iterated-group ► [iterated-group [n → [translate [point 0 [× n 10]] [box 10 10]]] 5] ►

Using iterated-group ► [iterated-group [n → [translate [point [× n 10] [× [sin [ ⁄ n 2]] 30]] [box 10 10]]] 20] ►

What about this one? ► [iterated-group [m → [iterated-group [n → [translate [point [× m 10] [× n 10]] [box 10 10]]] 5] 5] ? ►

What about this one? ► [iterated-group [m → [iterated-group [n → [translate [point [× m 10] [× n 10]] [box 10 10]]] 5] 5] ►

Conditionals: the if operator [if test consequent alternative] If test is true, Then evaluate and return consequent Otherwise, evaluate and return alternative Some useful tests [eq? a b] Checks if a and b are the same object [= a b], [> a b], [≤ a b], etc. Compares numbers [and test 1 test 2 … test n ] [or test 1 test 2 … test n ] [not test] Combines tests into more complicated tests ► [define abs [n → [if [> n 0] n [- n]]]] ► [abs 5] 5 ► [abs -5] 5 ►

Boolean objects Everything in Meta is an expression, And (almost) all expressions have values, So then what kind of value is [= a b] ? Answer: true or false The system includes two magic data objects, true and false, which are used to represent the answers to tests