[Video] and so on... Problems that require a function definition can be phrased as a word problem such as the following:

Similar documents
Animations involving numbers

Once you define a new command, you can try it out by entering the command in IDLE:

Table of Laplace Transforms

Have the students look at the editor on their computers. Refer to overhead projector as necessary.

Name: Bootstrap:2. Class:

Animations that make decisions

Project 2: How Parentheses and the Order of Operations Impose Structure on Expressions

Chapter 1 Operations With Numbers

Logical Connectives. All kittens are cute. ; I like pizza. ; The sky is blue. ; Triangles have three sides.

Making a maze with Scratch

Name: Tutor s

Lecture 1: Overview

Self-Teach Exercises: Getting Started Turtle Python

Boolean Expressions. Is Equal and Is Not Equal

Interactive Tourist Map

Create Turtles with Python

Working with images and scenes

Tips and Guidance for Analyzing Data. Executive Summary

Electrical Circuits and Random Walks: Challenge Problems

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

OrbBasic Lesson 1 Goto and Variables: Student Guide

Textures and UV Mapping in Blender

OrbBasic 1: Student Guide

Lab # 2. For today s lab:

1 Introduction to Matlab

OrbBasic LESSON 1 Goto and Variables Student Guide

Heuristic Evaluation of [ Quest ]

SPRITES Moving Two At the Same Using Game State

1 Getting started with Processing

Homework: Simple functions and conditionals

POSTER PROBLEMS LAUNCH POSE A PROBLEM WORKSHOP POST, SHARE, COMMENT STRATEGIC TEACHER-LED DISCUSSION FOCUS PROBLEM: SAME CONCEPT IN A NEW CONTEXT

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

x 2 + 3, r 4(x) = x2 1

Intro to Python Programming

Digitizer Leapfrogging

Adding content to your Blackboard 9.1 class

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

Depending on the computer you find yourself in front of, here s what you ll need to do to open SPSS.

Boolean Expressions. Is Equal and Is Not Equal

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

APPM 2460 Matlab Basics

Close Your File Template

[ the academy_of_code] Senior Beginners

Minerva. Quick Start Guide

Lecture 8 Data Structures

Interactive Tourist Map

Part II Composition of Functions

Name: Student Workbook. Class:

NCMail: Microsoft Outlook User s Guide

Section 4.1: Introduction to Trigonometry

Lesson 3.1. Midpoint and Dilations (5.1.1, 5.2.1, and 5.2.2) Pages 8-38 in Unit 5 Workbook

Chapter One: Getting Started With IBM SPSS for Windows

The first program: Little Crab

Quick Guide. Choose It Maker 2. Overview/Introduction. ChooseIt!Maker2 is a motivating program at first because of the visual and musical

Arduino IDE Friday, 26 October 2018

EXCEL BASICS. Helen Mills META Solutions

BB4W. KS3 Programming Workbook INTRODUCTION TO. BBC BASIC for Windows. Name: Class:

Open Book Format.docx. Headers and Footers. Microsoft Word Part 3 Office 2016

Teach Yourself Microsoft Word Topic 12 - Multipage Document Features Part 1

Grade 2 I Can Math Statements

NCMail: Microsoft Outlook User s Guide

Inspire Ten Minute Task #1

Outlook Web Access. In the next step, enter your address and password to gain access to your Outlook Web Access account.

AppleWorks Tips & Tricks

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Lesson 1 - Getting Started

SCRATCH MODULE 3: NUMBER CONVERSIONS

4. Write sets of directions for how to check for direct variation. How to check for direct variation by analyzing the graph :

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

A Document Created By Lisa Diner Table of Contents Western Quebec School Board October, 2007

The Anglemaker Program

Name: Bootstrap:Reactive. Class:

(Refer Slide Time 6:48)

Working with Windows Movie Maker

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

(6.6) Geometry and spatial reasoning. The student uses geometric vocabulary to describe angles, polygons, and circles.

You just told Matlab to create two strings of letters 'I have no idea what I m doing' and to name those strings str1 and str2.

AREA Judo Math Inc.

PaintPot. Figure 2-1. The PaintPot app

1: Introduction to Object (1)

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

Adobe InDesign CC Tutorial Part 1. By Kelly Conley

INVESTIGATE: PARAMETRIC AND CUSTOMIZABLE MODELS

Introduction to Matlab

In this lesson you are going to create a drawing program similar to Windows Paint. 1. Start with a new project and remove the default cat sprite.

Sorting and Filtering Data

FROM A RELATIONAL TO A MULTI-DIMENSIONAL DATA BASE

Touring the Mac. S e s s i o n 3 : U S E A N APPLICATION

Section 0.3 The Order of Operations

On the Web sun.com/aboutsun/comm_invest STAROFFICE 8 DRAW

Section 2. Sending s

Grade 6 Math Circles October 16 & Non-Euclidean Geometry and the Globe

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Basics. Mouse The mouse normally looks like a little arrow, but it can change depending on what you are doing

Programming Robots with ROS, Morgan Quigley, Brian Gerkey & William D. Smart

Working Data Magic with Calculations

Lab #9: Introduction to Logisim CS 0447/COE 0147: Spring 2012

Matlab Tutorial 1: Working with variables, arrays, and plotting

Lecture (01) Getting started. Dr. Ahmed ElShafee

Excel VBA. Microsoft Excel is an extremely powerful tool that you can use to manipulate, analyze, and present data.

Transcription:

Defining Functions (Time 20 minutes) Defining a value is helpful when a program has lots of identical expressions. Sometimes, however, a program has expressions that aren t identical, but are just very similar. A program that has fifty solid, green triangles can be simplified by defining a single value, as long as they are all the same size. But what if a program has fifty green triangles of different sizes? [Video] Think about the Image functions you have already used, like star and circle. They take inputs and produce images. Similarly, we might want a green-triangle function that takes the size as an input and produces a green triangle. The programming language doesn t provide this function, but it does let you define your own functions. We want to define our own function (let s call it gt, for green triangle) that takes in a Number and produces a solid green triangle of whatever size we want. (gt 10) would be a shortcut for (triangle 10 "solid" "green") (gt 20) would be a shortcut for (triangle 20 "solid" "green") (gt 1980) would be a shortcut for (triangle 1980 "solid" "green") (gt 98) would be a shortcut for (triangle 98 "solid" "green") and so on... To make this more concrete, have a student "act" as gt. To call the function, another student says "gt ten!" (calling out both the name of the function and the input). The actor responds "triangle ten solid green", to signify the work that the function does when it receives an input. Problems that require a function definition can be phrased as a word problem such as the following: Define a function gt, which takes in a Number and produces a solid, green triangle of the given size. Luckily, we can follow specific steps to define functions from word problems. Let s work through the steps to define gt. Page 11 of 19

Step 1: Write the Contract The first step in defining a function is to write its Contract. Contracts summarize three pieces of essential information about a function: The Name of the function: in this case, the name is given, as gt The Domain of a function, which is the types of data that the function expects: in this case, just a single Number. The Range of this function, which is the type of data that the function produces: in this case an Image since it produces solid, green triangles Here s the gt Contract written as code. The line starts with a semicolon, followed by the name, a colon, the Domain, an arrow, then the Range: It is often a good idea to give students examples of different word problems, and have them pick out the contract for each one. Contracts are written as comments in Racket: whenever Racket sees a semicolon, it ignores the rest of the line after the semicolon. This means that you will never get an error message from Racket for a malformed comment. That also means that you have to check your students contracts more closely, because the computer will not check anything about them (format or contents). Word problems give several clues as to the name, Domain, and Range of a Be sure to read the problem carefully! Some word problems will describe functions that take multiple inputs in their Domain, or inputs of different types. Open your workbook to Page 9, where it says "fast functions", and write the Contract for the gt Page 12 of 19

Step 2: Give Examples It s always a good idea to think through a few examples before defining the Examples show the shortcut that a function is trying to provide. This programming language provides a special construct, called EXAMPLE, which helps you write down how the function is used and what it should compute. You can see two such examples here, written under the contract: (EXAMPLE (gt 100) (triangle 100 "solid" "green")) These examples tell the computer that writing (gt 50) should produce the same result as (triangle 50 "solid" "green"), and that (gt 100) is equivalent to (triangle 100 "solid" "green"). The word problem specifies that the examples must use the name gt, and must all produce solid, green triangles. In your workbook, write two examples of your own for this Be sure to point out that EXAMPLE is capitalized, and that all examples are written in the definitions area. Many students will follow along here without really understanding, simply by patternmatching. Be sure to ask them lots of questions, to have them justify each step: Why does the example have to start with gt? (Because it s the Name of the function, specified in the contract) How do we know gt requires only one number? (Because it s the Domain of the function, specified in the contract) How do we know to use triangle? (Because the word problem tells us what shape it has to produce) How do we know the triangle has to be solid and green? (Because the word problem tells us what shape it has to produce) How do we know the correct order for the inputs to triangle? (The contract for triangle tells us) One of the big ideas here is that each step informs the subsequent step. Make sure to explicitly connect them for students, pointing out that the Contract gives strong hints about how to write each part of the examples. Page 13 of 19

Programmers often write several examples for each Examples like these are a way for a programmer to think through their work. Examples also make it easy to look at what parts of the expression can change, or vary, depending on the inputs. Write the following examples on paper and circle the parts of the examples that are different: (EXAMPLE (gt 100) (triangle 100 "solid" "green")) Pay close attention to what students circle: they should circle something in each part of the Example (the function use on the left and the expression on the right); they should also use the same name for the same concept across the expressions. Their circles will correspond to the variables in their functions, so the variables should appear in both the left and the right sides of the Example. Once you know which parts of the expression change, label the circles with a name that describe their contents. For example, in our two gt examples, we have circled the size of the triangle. Your circled and labeled expressions should look like the following diagram: Step 3: Define the function After writing the Contract, two Examples, and the labeled circles, defining the function itself is relatively simple. Copy everything that stays the same (everything that wasn t circled) in one of your EXAMPLE lines (onto paper or into your editor) In place of each circle, write the label you gave to that circle Change EXAMPLE to define This can be a good opportunity to point out that the parts of the examples that were changeable (or vary-able) are what determines when we need to use the variable. (EXAMPLE (gt 100) (triangle 100 "solid" "green")) (define (gt size) (triangle size "solid" "green")) Page 14 of 19

On your paper, define the gt function, then type the Contract, Examples and Definition into the Definitions area. Click "Run", to have the computer read this definition. Use the function you ve defined, by typing (gt 100) in the Interactions area. Try using the function with different Numbers Your answer should look something like this.: http://www.wescheme.org/openeditor?definitionstext=; gt : Number - > Image%0A %0A (EXAMPLE (gt 95) (triangle 95 "solid" "green"))%0a (define (gt size) (triangle size "solid" "green")) (EXAMPLE (gt 95) (triangle 95 "solid" "green")) (define (gt size) (triangle size "solid" "green")) ] Page 15 of 19

These steps are knows as the Design Recipe, which is a powerful tool for defining functions based on word problems. Practice: Write a function bc, which takes in a Number and produces a solid, blue circle of the given size. In your workbook (still on Page 9), fill out the Contract for this What is the function s Name? What is the function s Domain? What is the function s Range? Using the Contract you ve written, write two Examples for the What part of the Contract helps you fill in the left side of an Example? What part of the Contract tells you what the function needs as an input? How can the Range of a function help you write the Example? Looking at those two examples, circle the parts that are changeable, then label them with a good variable name. Is the variable name you chose the same as the one you chose for gt? Why or why not? Why is it helpful to choose a variable name before defining the function? Now write the function definition, using the Examples you ve written. You will want to explicitly connect each step in the Design Recipe to every other step. Ask students to justify each part of their Contract by referring back to the Word Problem, to justify each step of their Examples by referring back to the Word Problem and Contract, and finally to justify each step of the definition by referring to the Examples. The same variable name can be used in multiple functions, just as in math (where many functions use x as the variable name, for example) Thinking through the word problem step-by-step, we arrive at: ; bc : Number -> Image (EXAMPLE (bc 16) (circle 16 "solid" "blue")) (EXAMPLE (bc 421) (circle 421 "solid" "blue")) (define (bc radius) (circle radius "solid" "blue")) Page 16 of 19

Practice: Write a function dot, which takes in a Color and produces a solid circle of the given color, with a radius of 15. In your workbook (still on Page 9), fill out the Contract for this What is the function s Name? What is the function s Domain? What is the function s Range? Using the Contract you ve written, write two Examples for the function, then circle and label the variables. What part of the Contract helps you fill in the left side of an Example? What part of the Contract tells you what the function needs as input? How can the Range of a function help you write the Example? What is a good variable name for what changes between these Examples. Now write the function definition, using the Examples you ve written. Thinking through the word problem step-by-step, we arrive at: ; dot : String -> Image (EXAMPLE (dot "red") (circle 15 "solid" "red")) (EXAMPLE (dot "blue") (circle 15 "solid" "blue")) (define (dot color) (circle 15 "solid" color)) Page 17 of 19