You define procedures with (define name procedure-expression)
.
(lambda (name) expression)
.(o f2 f1)
, which yields a
procedure that applies f1
and then applies f2
to the result.(section proc <> constant)
or something similar.a. If you have not done so already, you may want to open separate tabs or window in your browser for the four readings.
b. If you have not done so already (or if it didn’t work the last time), open a terminal window and type the following (without the dollar-sign prompt).
$ /home/rebelsky/bin/csc151/setup
If you can’t remember whether or not you typed the command, there should be no harm to typing it again.
c. We are still in the process of writing the loudhum
library
that we will be using throughout the semester. It’s a good habit
to try to grab the latest version of that library. If you did not
just run the setup
command, open a terminal window and type the
following (without the dollar-sign prompt).
$ /home/rebelsky/bin/csc151/update
d. In the Definitions pane, add instructions to load the loudhum
and 2htdb/image
libraries.
#lang racket
(require loudhum)
(require 2htdp/image)
e. Add the following definitions to your Definitions pane. (When copying procedure definitions, please include any associated documentation.)
;;; Procedure:
;;; color-square
;;; Parameters:
;;; side-length, a non-negative integer
;;; color, a color
;;; Purpose:
;;; Create a solid square in the given color.
;;; Produces:
;;; square, an image
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; * square, when rendered, has the given side length and color.
;;; * (image-width square) = side-length
;;; * (image-height square) = side-length
(define color-square
(lambda (side-length color)
(rectangle side-length side-length 'solid color)))
;;; Value:
;;; red-square
;;; Type:
;;; image
;;; Contents:
;;; A small red square, useful for exercises in this lab.
(define red-square (color-square 20 'red))
;;; Value:
;;; black-square
;;; Type:
;;; image
;;; Contents:
;;; A small black square, useful for exercises in this lab.
(define black-square (color-square 20 'black))
f. Save your definitions on the Desktop as procedures-lab.rkt
.
The reading on procedures contains a procedure to make a simple house.
a. Copy that procedure into your Definitions pane.
b. Verify that you can create houses of different sizes. You may choose to do the same examples as in the reading or you may choose to do a few of your own.
c. The original simple-house
procedure does not color the house.
Write a new procedure, (painted-house size color)
, that takes
both the size and color as parameters and makes a house of the
specified size and given color.
The reading on images contained an image that looked a bit like a snowman.
a. Write a procedure, (snowman size)
that creates a simple snowman
with three white circles with black outlines, where the size is used
for the size of the largest part of the snowman (the base).
b. Write a procedure, (snowman-revisited height)
that creates a simple
snowman with three white circles with black outlines, where the
total height of the snowman is approximately height
. (It should
be as close to height
as you can get it.)
c. Write a procedure, (colorful-snowman height color)
, that
behaves like snowman-revisited
, except that the snowman is drawn
in the desired color.
d. Write a procedure, (snowman-with-hat height)
, that
behaves like snowman-revisited
, except that it adds a black top
hat to the top of the snowman.
Although we’ve added a top hat to a snowman, we might also want to
add top hats to arbitrary images. Write a procedure, (add-top-hat
width height image)
, that takes a width and height of the top hat
as paramters, creates a top hat of the given size, and puts it on
top of the image.
The reading on procedures also contained a
more complex drawing of a house, one with a door and a doorknob.
Write a new procedure, (house size color)
that makes a house of
the given size and color, including a brown door and a yellow
doorknob.
Someone designed the code for the house with the brown door and yellow doorknob. Although we have modified and generalized the code, we should take time to cite the code that we borrowed and modified. Add such a citation. For example,
;;; Props:
;;; The design of the house comes from a CSC 151 reading on
;;; procedures, written by Curtsinger et al. and available at
;;; <FILL IN THE URL>.
a. Write a procedure, (grid image)
, that takes the given image and
makes a two-by-two grid with that image.
> (grid (circle 10 'solid 'blue))
> (grid (star 10 'solid 'red))
b. Using grid
, create a checkerboard with as little code as
possible. To get you started, here’s a two-by-two checkerboard.
(define two-by-two
(above
(beside red-square black-square)
(beside black-square red-square)))
a. Using section
(and no lambda
), write a procedure,
(red-circle size)
, that creates a solid red circle of the
specified size.
b. As you may know, some modern artists, like Andy Warhol, achieved
interesting conceptual pieces by creating grids of the same (or similar)
thing. Using composition and section (and no lambda
), write a
procedure (sixteen-circles color)
, that makes a grid of sixteen
equal-size solid circles of the given color.
Note: You can use your grid
procedure from the previous exercise.
As you may recall, we used the following code to count the number of words in a string.
;;; Procedure:
;;; count-words
;;; Parameters:
;;; str, a string
;;; Purpose:
;;; Determine how many words appear in string.
;;; Produces:
;;; count, a non-egative integer.
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; count represents the number of words in the string.
;;; Note that a "word" is any sequence of characters separated
;;; by a space.
(define count-words
(lambda (str)
(length (string-split str " "))))
a. In your own words, explain how count-words
works.
b. Check the behavior of count-words
on a few inputs.
Using a lambda, write a procedure, (count-letter str letter)
,
that counts how many times the letter appears in str
. For example,
> (count-letter "The vorpal sword went snicker snack" "o")
2
> (count-letter "The vorpal sword went snicker snack" "s")
3
> (count-letter "The vorpal sword went snicker snack" "z")
0
Hint: Think about the definition of count-words
.
Using composition and sectioning, write a procedure, (count-zeds str)
,
that counts the number of times the letter “z” appears in the given string.
Add the following definition in the Definitions pane.
(define logo
(lambda (size)
(overlay
(rectangle (* (/ 1 (sqrt 2)) size)
(* (/ 1 (sqrt 2)) size)
'solid 'black)
(circle (* 1/2 size) 'solid 'red)
(rectangle size size 'solid 'black))))
a. Make a mental sketch of what that logo looks like.
b. Verify the appearance in the Interactions pane.
c. Add the following to the interactions pane.
> (define combo-a
(beside/align 'bottom (logo 20) (logo 25) (logo 30) (logo 25) (logo 20)))
> (define combo-b
(lambda ()
(beside/align 'bottom (logo 20) (logo 25) (logo 30) (logo 25) (logo 20))))
d. combo-a
and combo-b
are similar, but slightly different. What type
of thing is combo-a
? What type of thing is combo-b
?
If you find that you complete the laboratory with time to spare, you might consider doing one or more of the following exercises.
As you may have noted, your colorful-snowman
procedure from
exercise 2 creates an invisible snowman if the color is white.
Write a new procedure, (better-snowman height color)
that draws
a snowman in the given color with every circle outlined in black.
We’ve already added a top-hat to our snowman. Add some other features of your choice.
Extend your house
procedure to add more features (e.g., a window),
additional parameters (e.g., the height and width of the house, rather
than the strange “size”), or both.)
In the reading on images, we challenged you to create a smiley face. Write a procedure that creates a smiley face of a specified size.
The checkerboard example comes from a very old version of CSC 151. I’m not sure which member of the department wrote it.
Bits and parts of this lab come from procedure labs from other versions of CSC 151.