Вы находитесь на странице: 1из 4

CMPS10: Introduction to Computer Science

Homework 2: Lightbot in Symbolic Form


Goal: The purpose of this assignment is to look at textual ways of programming the Lightbot and to
learn how to express a function symbolically.
When we solved the Lightbot 2.0 exercises last time, we programmed the bot using a tiny list of
instructions presented iconographically, that is, as pictures. But, these could just as easily be written
symbolically, that is, as text. So, our instruction list

can be expressed symbolically,


Step

Right

Left

Jump

Power

F.name

as shown. When we program symbolically, we dont have to name the functions F1 and F2. We can
give them actual names like F.turn_around for a function that causes the bot to turn around. So,
we can give the symbolic solution to the following Lightbot problem as
Left, Step, Right, Step, Step, Step, Step, Right, Step, Power.

Symbolic expression is the same thing, just a different form with slightly different properties as we
now see.

Iteration
We notice that often in programming the bot we need to repeat operations, such as the four steps in the
example. (We previously used recursion to repeat instructions, but there is another way.) Wouldnt it be
easier if instead of all of the dragging or typing, we just wrote 4Step or Step4 or some other similar
form? Yes, of course. So, we will use the form 4:Step to mean to do the Step operation 4 times;
more generally we write <number>:<operation>, where the operation is one of our basic
instructions given above like Step, or perhaps a list of them grouped inside of a parenthesis pair, as in
3:(Jump, Power, Step, Step), which will do the four-instruction sequence 3 times.

Symbolic Function Definitions


When we define functions symbolically, we use a special form. For the function definition, that is,
saying how it works, we write the name, a pair of parentheses, the operation sequence, and a final
period. (The purpose of the parentheses will be clear later.) For example, this is a function definition
for a function that turns the bot around:
F.turn_around() Right, Right.
name

body

The name of the function is the part between the dot and the open parenthesis, and the part after the
closing parenthesis to the period, is called the body. It defines how the function works.
When we use a function, its called the function call or function invocation, we also have a special form.
We give the name, followed by the parentheses, as in turn_around(), which instructs the bot to do
the instructions in the body of the function. For example, to program the bot to turn around and jump,
we would write the program turn_around(), Jump.
When the bot gets these instructions, it runs the function by checking the function definition, and
following its instructions of the function body; when complete it returns to do the instructions
following the call.
Important. Notice that a single function like turn_around() has two roles: in one role it is defined;
in the other role it is called. A function need only be defined once, because its necessary to tell how it
works only once. A function can be called many times, because every time we need the operation it
performs, we call it.

Example: The Moon Walk


Being a rock music listener and a fan of the late Michael Jackson, the Lightbot 2.0 likes to do the
Moonwalk. If we tell the bot, when its standing on a raised block, to walk, its legs and arms move, but
it doesnt go anywhere. This leads to the bot version of the Moonwalk. This is the Moonwalk function
definition:
Function Definition
F.moon_walk() 4:(Step, Right).
According to the function body, the bots Moonwalk is four repeats of taking a step (and not going
anywhere) and then turning right. So, it walks in each direction for one step. To use the moon_walk()
function after jumping up two steps, for example, we could write
Main Method
Jump, Jump, moon_walk().
This is an example of calling the moon_walk() function. The program asks the bot to up twice, and
then call the moon_walk() function, which causes it to do the operations in the functions definition.

Exercises
Part 1. Give symbolic programs for the following Lightbot problems (1a), (1b) and (1c), writing them
with a word processor (such as Microsoft Word, or any simple text editor). If iteration can be profitably
used, use it. Caution: Check your work to be sure youve got it right.

(1a)

(1b)

(1c)

(2b)

Part 2. You will write three functions in this section; for the last one, the bot does the Moonwalk.
(a) Consider a different solution to the problem (1c) from Part 1. Suppose we have written the program
7:light_a_pair(). Define that is, write out the function definition for the
F.light_a_pair()function so that the command works for our program.
(b) The program for problem (2b) ends with the instruction 4:light_a_side().
Main Method
Jump, Left, Jump, Right, 4:light_a_side().
Write the F.light_a_side()function definition so that the program works. Your function will
probably take about seven instructions.
(c) The Moonwalk The Lightbot wants a new solution to the Basic Level 6 of the Lightbot 2.0. (You
solved Level 6 in assignment 1. Find it at http://armorgames.com/play/6061/light-bot-20.) What the bot
wants is to go up each riser (see 2c) and do its version of a Moonwalk on the top before powering the

light. (As explained above the bots Moonwalk exploits the fact that the bot cannot walk forward if
there are no tiles in front of it so it just walks in place.) You are welcome (encouraged) to make a
different function definition for the Moonwalk than above.
Solve problem (2c) so that the bot does a Bot Moonwalk at the top of each riser before powering the
light. (You will probably want to use another function to simplify your work.) Include a copy of your
moon_walk() definition with your solution.

(2c)

Wrap Up
In this assignment you learned about iteration, the symbolic representation of instructions and how to
express functions symbolically. There is a function definition, with a three-part structure name,
parentheses, body and a function call, also with a standard form with parentheses at the end. You
wrote functions and a program to demonstrate your understanding of using these concepts, which you
will use extensively in the future.

Submit
The solutions to all six problems should be written out in a text document. The only acceptable file
types are .doc, .docx, .rtf, .txt, or .pdf. Please name your file hw2.doc, or hw2.txt etc. depending on the
proper file type. Submit the document on the https://ecommons.ucsc.edu/ site as per instructions on
Homework 1.

Вам также может понравиться