We know have to navigate between Karel s World view, Karel s Program view and Karel s Execution (or Run) view.

Similar documents
We know how to navigate between Karel s World view, Karel s Program view and Karel s Execution (or Run) view.

CS112 Lecture: Introduction to Karel J. Robot

Jerry Cain Handout #5 CS 106AJ September 30, Using JSKarel

CS112 Lecture: Extending Classes and Defining Methods

Laboratory 1: Eclipse and Karel the Robot

Slide 1 CS 170 Java Programming 1 Testing Karel

Programming in Karel. Eric Roberts and Jerry Cain CS 106J April 5, 2017

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Mehran Sahami Handout #7 CS 106A September 24, 2014

Numeric answers: select letters to total to your answer. For example 3 = d and e, 11 = b, d and e & 17 = a and e. a) 16 b) 8 c) 4 d) 2 e) 1

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Using Karel with Eclipse

CS 106A, Lecture 3 Problem-solving with Karel

IS12 - Introduction to Programming

PreTest.doc. That was so much fun, lets do it again :^) Name Period. 1 Beeper. 25. How many Beepers are in Karel BeeperBag?

CS 106A, Lecture 3 Problem-solving with Karel

Shorthand for values: variables

IS12 - Introduction to Programming. Lecture 6: Advanced Programming with Karel. Peter Brusilovsky

Using Eclipse and Karel

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

Learning Programming with Robots

Working with Track Changes: A Guide

Singly linked lists in C.

Guide - The limitations in screen layout using the Item Placement Tool

CS112 Lecture: Defining Instantiable Classes

Debugging with Karel

6.001 Notes: Section 15.1

Basic Reliable Transport Protocols

Advanced Operations Research Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology, Madras

Practice Midterm Examination #1

COPYRIGHTED MATERIAL. Dipping Your Toe into Python. Part I. Chapter 1: Programming Basics and Strings. Chapter 2: Numbers and Operators

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

A Step-by-Step Guide to getting started with Hot Potatoes

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

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

Before we start: Any questions on Karel?

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Detailed instructions for video analysis using Logger Pro.

CS 1567 Intermediate Programming and System Design Using a Mobile Robot Aibo Lab3 Localization and Path Planning

Table of Laplace Transforms

Functions that return lists

OrbBasic Lesson 1 Goto and Variables: Student Guide

Homework 09. Collecting Beepers

AP CS A Karel J Robot Review Sheet Chapters 5-6

Trombone players produce different pitches partly by varying the length of a tube.

Developing and Reusing Methods

5 R1 The one green in the same place so either of these could be green.

The compiler is spewing error messages.

OrbBasic 1: Student Guide

Excel Basics: Working with Spreadsheets

Chapter 1. Getting Started

The first program: Little Crab

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Introduction. Why Use Forms? Access 2010 Working with Forms. Page 1

Heap Arrays and Linked Lists. Steven R. Bagley

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

Test-Driven Development (TDD)

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

6.001 Notes: Section 8.1

15 Minute Traffic Formula. Contents HOW TO GET MORE TRAFFIC IN 15 MINUTES WITH SEO... 3

Your . A setup guide. Last updated March 7, Kingsford Avenue, Glasgow G44 3EU

Using GitHub to Share with SparkFun a

COMP 161 Lecture Notes 16 Analyzing Search and Sort

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

#30: Graph theory May 25, 2009

Mastering Truspace 7

ASSIGNMENT 4 Classes and Objects

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

MAPLOGIC CORPORATION. GIS Software Solutions. Getting Started. With MapLogic Layout Manager

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Architectural Documentation 1

Practice Midterm Examination #2

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

Linked Lists. What is a Linked List?

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

An Interesting Way to Combine Numbers

Multiple Variable Drag and Drop Demonstration (v1) Steve Gannon, Principal Consultant GanTek Multimedia

Valuable points from Lesson 6 Adobe Flash CS5 Professional Classroom in a Book

(Refer Slide Time: 01.26)

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Unit 9 Tech savvy? Tech support. 1 I have no idea why... Lesson A. A Unscramble the questions. Do you know which battery I should buy?

Page design and working with frames

Every function machine takes its inputs from a bag called the Input bag. Here are some examples of this function machine's Input bag:

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

You might already know that tables are organized into vertical columns and horizontal rows.

the NXT-G programming environment

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Storing Data in Objects

1. Mesh Coloring a.) Assign unique color to each polygon based on the polygon id.

Digitizer Leapfrogging

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 37 Resolution Rules

T H E I N T E R A C T I V E S H E L L

Download Free Pictures & Wallpaper from the Internet

Defrosters: Cyndi Ai, Esther Chen, Ben Schiffler, Sean Yang Assignment 3d: Usability Testing Review May 17, Overview

A simple problem that has a solution that is far deeper than expected!

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

If Statements, For Loops, Functions

Introduction to Programming Style

Permissions Tutorial: Add a Read-Only User

Transcription:

We know how to write programs using Karel s primitive commands move turnleft pickbeeper putbeeper turnoff We know have to navigate between Karel s World view, Karel s Program view and Karel s Execution (or Run) view. We know how to Compile a program, which means to translate it from Karel s programming language, which humans can understand as well as Karel into machine code that the computer inside Karel understands. AND, writing programs with only the basic 5 primitives is hard to keep track of exactly what we are asking Karel to do. And it is tedious. We have already seen how Karel can to go to a box with a beeper inside and retrieve the beeper and bring it home. The World view looked something like this: It was painful enough to keep track of all the move instructions and all the turnleft commands when Karel was close to the Origin (Home Base). Imagine if Karel s World looked like this:

We are solving essentially the same problem, retrieve the beeper from the box and come home, but now the number of move instructions makes the task incredibly tedious. This is where the concept of creating new instructions for Karel becomes essential. NING-OF-PROGRAM NING-OF-EXECUTION instructions END-OF-EXECUTION END-OF-PROGRAM Creating a new instruction is not a difficult task, and it is not complex. It does require care and attention to detail. The benefit to the human in charge or Karel (the programmer) is that the program can be much more natural and easy to understand. To create a new instruction for Karel we use this command: DEFINE-NEW-INSTRUCTION <new name here> AS <instruction>

Now, technically only one single instruction can follow the AS If that were really true, Karel would never get any easier to use. While technically that is true, functionally there is an easy way around it. Let s try a simple new definition: DEFINE-NEW-INSTRUCTION advance AS Our Karel programming language now contains the five primitives and the new instruction advance This is not an especially useful new instruction because we could have used move every time we used advance. To build powerful new vocabulary for Karel we can replace the word instruction below with a group of instruction bounded by or captured between the words and END. DEFINE-NEW-INSTRUCTION <new name here> AS <instruction> DEFINE-NEW-INSTRUCTION turnright AS Look at the following program:

We have created two new instructions to increase Karel s vocabulary. As we have seen, one helps us very little. The advance instruction is identical to move and really adds nothing to Karel s vocabulary. But it is a perfectly legal new instruction. On the other hand, the turnright instruction is very useful. It gives us, the programmers a shorthand for having to over-and-over-again type turnleft three times every time we want Karel to make a right turn. Where do we put these new instructions and what form must they take? beginning-of-program Put the definition of new instruction here beginning-of-execution Primitives Use newly defined instructions turnoff; end-of-execution end-of-program Let s enter our definitions.

beginning-of-program DEFINE-NEW-INSTRUCTION turnright AS beginning-of-execution turnright; turnoff; end-of-execution end-of-program Now notice the instruction list as Karel is Executed. As Karel s programmer, the inclusion of the instruction turnright has made our tasks simpler. But, nothing has really changed for Karel. Every time the turnright instruction is used in our program Karel, simply looks up the new instruction, follows the definition exactly and then moves on. Control moves TEMPORARILY from our list of instructions between beginning-of-execution and end-of-execution to the definition section and then back again. Notice if our program had two turnright commands Karel would actually follow look up the definition twice and each time do exactly what it was told.

control has moved from the list of instruction to the turnright definition. When Karel finishes that instruction by turning left three times, control will move back to the main part of the program. Has Karel really learned a new instruction? No, every time Karel encounters the turnright command, he simply does turnsleft three times. However, as programmers we have gained flexibility. Now our programs can better reflect what we really want Karel to do. Let s look at another Example.

In this World, we want Karel to retrieve a beeper and bring it home. Karel is fairly far away from the beeper. The beeper is at 10 th Street and 8 th Avenue. Karel is at the Origin (Home Base) 1 st Street and 1 st Avenue. We need to use lot of move instructions to get Karel to the beeper and back. Problem Statement: Karel starts at the Origin (Home Base) and needs to travel to 10 th Street and 8 th Avenue to retrieve a beeper. Karel then needs to come home, drops beeper at HomeBase and face North. Define the Output: Get to 10 th Street and 8 th Avenue and pick up beeper. Then get home. Define the Input: Start at Origin(HomeBase) with empty beeper-bag. Initial Algorithm: 9 move instructions turnright 7 moves turn-around 7 moves turnleft 9 move instructions turn-around drop off beeper If we wrote out all the move instructions, and we could teach Karel what turnright and turn-around means. This program would work. Refine Algorithm: Define turnright as 3 turnleft instructions Define turn-around as 2 turnleft instruction ------ 9 move instructions turnright 7 moves turn-around 7 moves turnleft 9 move instructions turn-around drop off beeper The following program is exactly our refined algorithm written very carefully, using all the appropriate punctuation and headings required by Karel s language. AND, as instructed by our refined algorithm, we have defined turnright and turn-around.

beginning-of-program DEFINE-NEW-INSTRUCTION turnright AS DEFINE-NEW-INSTRUCTION turn-around AS beginning-of-execution turnright; pickbeeper; turn-around; This main program has 39 statements turn-around; putbeeper; turnoff; end-of-execution end-of-program This set of instructions solves our problem. It is as straightforward as it can get. It also has so many move instructions that it is easy to type in either too many or too few and then have to keep fixing out code.

We used 9 move instructions, followed by 7 move instructions, followed by 9 more and then 7 more. If we created a move4 instruction, our code would be little different for Karel, but much simpler for us. What would our move4 instruction look like? DEFINE-NEW-INSTRUCTION move4 AS This is essentially another Refinement of our Algorithm Refine Algorithm Again: Define turnright as 3 turnleft instructions Define turn-around as 2 turnleft instruction Define move4 as 4 move instructions ------ 2 move4 instructions 1 move instruction Turnright 1 move4 instruction 3 moves turn-around 1 move4 instruction 3 moves turnleft 2 move4 instructions 1 move instruction turn-around drop off beeper While our Refined Algorithm may look a bit longer, the actual program will be much shorter. This is what our revised program would look like.

beginning-of-program DEFINE-NEW-INSTRUCTION move4 AS DEFINE-NEW-INSTRUCTION turnright AS DEFINE-NEW-INSTRUCTION turn-around AS beginning-of-execution move4; move4; turnright; move4; pickbeeper; turn-around; move4; move4; move4; turn-around; putbeeper; turnoff; end-of-execution end-of-program This main program has 21 statements By using the move4 instruction, our main program (the part of the program without the definitions) has been reduced from 39 instructions down to 21 instructions. More important, this new program is easier to read by humans and thus easier to understand. To simplify the main program we needed to carefully define three new instructions for Karel to use. We defined, turnright, turn-around and move4. Let s begin a totally new problem.

Karel s new World looks like this: Problem Statement: Karel is to go past each Hurdle, pick up the 3 beepers and place them behind the 4 th wall. Define Output: Three walls with beepers behind them, one wall at the end. Define Input: Karel is at 6 th Street and 1 st Avenue. The Hurdles/Walls are to Karel s right Initial Algorithm: Turnright Move 3 blocks Go around first hurdle Got around second hurdle Go around third hurdle Go around 4 th hurdle Drop off 3 beepers As with the last program, while this algorithm solves the problem there are a lot of words in the algorithm that Karel cannot understand because there is nothing in his limited vocabulary to describe them. We will have to define turnright AND go around hurdle for a start.

Refine Algorithm: Define turnright Define go-around-hurdle ------------------ Turnright Move 3 blocks Go around first hurdle Got around second hurdle Go around third hurdle Go around 4 th hurdle Drop off 3 beepers Let s look at the definition section. Turnright we have created before. Go-around-hurdle requires a new definition. And, this definition is not simply a number of the same terms repeated. Let s pretend that Karel has moved from the starting location at 6 th and 1 st to being just before the first hurdle. THINK! What do we need Karel to do? Creating definitions is often exactly the same as writing a program. What do we need Karel to do to get past a hurdle? Turnleft Move 1 street Turnright Move 1 street Turnright Move 1 street

This set of instructions seems to get Karel around the wall and ready to pickup the beeper. Let s enter the information into the Program section of Karel s screen and see if we ve given Karel enough information. We ve defined two new instructions. We ve defined turnright and Go-around-hurdle. NOTICE: We defined a new instruction that makes use of another instruction. IS this Legal? As long as an instruction is defined before it is used, one new instruction can use another. So, Go-around-hurdle could make use of the turnright instruction because it appears before the definition for Go-around-hurdle. beginning-of-program Put the definition of new instruction here beginning-of-execution Definitions MUST be in the right location of the program or Karel will not be able to find them.

Now that we have our definitions in place (turnright and Go-around-hurdle) we can write our program. Let s got back to our Refined Algorithm: Refine Algorithm: Define turnright Define go-around-hurdle ------------------ Turnright Move 3 blocks Go around first hurdle Got around second hurdle Go around third hurdle Go around 4 th hurdle Drop off 3 beepers Looking at the vocabulary in our algorithm, is there anything remaining that we cannot translate directly into Karel s language. There does not seem to be. So let s just type in the code using the primitive commands. In other words, there the algorithm says, move 3 blocks, we would have to type: Our total program includes what is visible in this screen shot and, the previously created definitions.

Here is the entire program, which compiles without errors in spelling, punctuation or misunderstood words. beginning-of-program DEFINE-NEW-INSTRUCTION turnright AS DEFINE-NEW-INSTRUCTION Go-around-hurdle AS turnright; turnright; beginning-of-execution turnright; Go-around-hurdle; pickbeeper; Go-around-hurdle; pickbeeper; Go-around-hurdle; pickbeeper; Go-around-hurdle; putbeeper; putbeeper; putbeeper; turnoff; end-of-execution end-of-program When you Execute your program, watch Karel. You will notice that Karel really doesn t remember the new vocabulary we have defined. Rather, each time Karel encounters a defined instruction, Karel looks it up does what it says and promptly forgets it again. So Karel doesn t really KNOW how to turnright. Karel does know how to look up the definition, do three turnleft instructions. However, by creating these instructions, our programs become easier for people to read.