9
* 4.1 Lesson 2: Defining Functions Lesson 2: Defining Functions ... * 4.2 What’s It All About? > What’s It All About? 1. This lesson features the definition of simple functions, where simple means no repetition or selection. The 2htdp/image library is featured. Random colors are generated. Images are incorporated into the programming. 2. Just two programs are considered. The first program paints a 3x3 checkerboard with black and grey squares, each containing a randomly colored dot. The second paints a 3x3 checkerboard based on some of Andy Warhol’s art. * 4.3 Colorful Little Checkerboard > Colorful Little Checkerboard A program is written to draw a 3x3 black and grey checkerboard in which each square of the board contains a randomly colored dot. Think something like this: * 4.4 Step 1 - Three Utility Functions >> Step 1 - Three Utility Functions The first step will be to define three little utility functions that will come into play when establishing components of the checkerboard. (One of the functions that appears below is just a little helper function.) * 4.5 Step 1 Development >>> Step 1 Development #lang racket ; Requirements

Lesson 2: Defining Functions

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lesson 2: Defining Functions

*

4.1 Lesson 2: Defining Functions

Lesson 2: Defining Functions

...

*

4.2 What’s It All About?

> What’s It All About?

1. This lesson features the definition of simple functions, where simple means no repetition or selection. The2htdp/image library is featured. Random colors are generated. Images are incorporated into the programming.

2. Just two programs are considered. The first program paints a 3x3 checkerboard with black and grey squares,each containing a randomly colored dot. The second paints a 3x3 checkerboard based on some of Andy Warhol’sart.

*

4.3 Colorful Little Checkerboard

> Colorful Little Checkerboard

A program is written to draw a 3x3 black and grey checkerboard in which each square of the board contains arandomly colored dot. Think something like this:

*

4.4 Step 1 - Three Utility Functions

>> Step 1 - Three Utility Functions

The first step will be to define three little utility functions that will come into play when establishing components ofthe checkerboard. (One of the functions that appears below is just a little helper function.)

*

4.5 Step 1 Development

>>> Step 1 Development

#lang racket

; Requirements

Page 2: Lesson 2: Defining Functions

; - Just the image library from Version 2 of "How to Design Programs"

( require 2htdp/image )

; Utilities; - A function to compute a given percent of a given number; - A function to halve a given number; - A function to generate a random color

( define ( half nr ) ( / nr 2.0 ) )( define ( percent pc nr ) ( * ( / pc 100.0 ) nr ) )( define ( rgb-value ) ( random 256 ) )( define ( random-color ) ( color ( rgb-value ) ( rgb-value ) ( rgb-value ) ) )

*

4.6 Step 1 Test/Demo

>>> Step 1 Test/Demo

Page 3: Lesson 2: Defining Functions

*

4.7 Step 2 - Problem Parameters

>> Step 2 - Problem Parameters

The second step involves establishment of the problem parameters. Names are given to the size of the board and anumber which scales the size of the dots that appear within the cells. The number of cells on each side of the boardis not named, since the program is not intended to be flexible in terms of the numbner of cells in the board.

*

4.8 Step 2 Development

>>> Step 2 Development

; Problem parameters; - We will consider the side length of the board to be the featured parameter; | and we will consider the board to contain just 3 cells (we don’t plan to; | afford flexibility with respect to the number of cells).; - From the side of the board, we will compute:; - - the side of a checkerboard square; - - the radius of the colorful circles that the squares will contain

( define side-of-board 200 )( define scale-factor 66.6666 )( define side-of-cell ( / side-of-board 3.0 ) )( define radius ( percent scale-factor ( half side-of-cell ) ) )

*

4.9 Step 2 Test/Demo

>>> Step 2 Test/Demo

> side-of-board200> scale-factor66.6666> side-of-cell66.66666666666667> radius22.2222>

*

4.10 Step 3 - Checkerboard Squares

>> Step 3 - Checkerboard Squares

The third step is all about generating the cells that will make up the checkerboard. Here are a couple of questionsto think about as you read this code:

1. Would you say that black-square-with-dot and grey-square-with-dot are functions from the perspectiveof programming in Racket?

2. Would you say that black-square-with-dot and grey-square-with-dot are functions in the mathematicalsense of the word?

Page 4: Lesson 2: Defining Functions

*

4.11 Step 3 Development

>>> Step 3 Development

; Colorful black cells and colorful grey cells; - Create basic black square and the basic grey square; - Establish a function to generate a black square with a colored dot; - Establish a function to generate a grey square with a colored dot

( define black-square ( square side-of-cell "solid" "black" ) )( define grey-square ( square side-of-cell "solid" "grey" ) )

( define ( black-square-with-dot )( define dot ( circle radius "solid" ( random-color ) ) )( overlay dot black-square )

)

( define ( grey-square-with-dot )( define dot ( circle radius "solid" ( random-color ) ) )( overlay dot grey-square )

)

*

4.12 Step 3 Test/Demo

>>> Step 3 Test/Demo

Page 5: Lesson 2: Defining Functions

*

4.13 Step 4 - The Checkerboard

>> Step 4 - The Checkerboard

The fourth step builds the checkerboard from the bottom up in terms of two kinds of row, black-grey-black andgrey-black-grey.

*

4.14 Step 4 Development

>>> Step 4 Development

; The checkerboard; - Function bgb-row draws a black-gray-black row of the checkerboard; - Function gbg-row draws a gray-black-gray row of the checkerboard; - These two functions are used to write a function to draw the checkerboard

( define ( bgb-row )( beside( black-square-with-dot )( grey-square-with-dot )( black-square-with-dot )

))

( define ( gbg-row )( beside( grey-square-with-dot )( black-square-with-dot )( grey-square-with-dot )

))

( define ( checkerboard )( above ( bgb-row ) ( gbg-row ) ( bgb-row ) )

)

Page 6: Lesson 2: Defining Functions

*

4.15 Step 4 Test/Demo

>>> Step 4 Test/Demo

Page 7: Lesson 2: Defining Functions

*

4.16 Checkerboard Variant Based on Some Warhol Art

> Checkerboard Variant Based on Some Warhol Art

A program is written to draw a 3x3 checkerboard in which each square of the board contains some flowers. Thinksomething like this:

What’s with the images embedded in the code shown on the following page? Consistent with the playful nature ofRacket, you can consider images to be atomic data. The simplest way to incorporate images into your program is bymeans of the Insert menu, which will let you grab images from your environment for use in your program. That iswhat I did for this little example. I clipped a couple of variants of a Warhol print, saved them to my machine, andthen inserted them at the prompt while typing in the two relevant define forms. This method is kind of crude, butit has the advantages of being easy and fast. To obtain images of the size I wanted to work with, I did the scalingthing with the scale function that is available in the library.

Questions:

1. What’s with the crude white borders in the “painting”?

2. Where did they come from?

3. Why did I leave them in the painting?

4. How might the program be changed so that the white boarders line up?

Page 8: Lesson 2: Defining Functions

*

4.17 Definitions Pane

>> Definitions Pane

Page 9: Lesson 2: Defining Functions

*

4.18 Interactions Pane

>> Interactions Pane