2 - Meeting Karel

If you have already installed the karel package, we are ready to start using it. We will execute the command library(karel) to be able to use it:

> library(karel)

First of all, we need to create a world so that Karel can move around. We do this with the instruction generar_mundo(), specifying the name of the world we want to use inside the parentheses and with quotation marks. The package includes several worlds that we will explore, for example:

> generar_mundo("mundo001")

As you can see in the image, Karel’s world has some elements:

Additionally, Karel can perform the following activities:

The actions written as mentioned above are expressions that we can understand, but for Karel to understand them, we have to respect the instructions in which they are implemented in R:

The parentheses indicate that the above instructions are functions in R, meaning that the actions Karel can perform are executed in R using functions. While we will study functions in more depth later on, for now, we can say that a function is a section of a program that performs a specific task and has a specific name.

Some functions are built into the programming language, for example, when we calculate a logarithm in the R console during our initial steps: log(27). log is the name of a function that is already in R and takes the logarithm of the number inside the parentheses.

Other functions are added when we load a package, as is the case with the four functions mentioned earlier; they come in the karel package. Other functions are created or programmed by people, as we will do very soon.

Some important observations about the functions that execute Karel’s actions in R:

Above all, it is important to remember that these functions do not work on their own. They must always be preceded by the execution of the generate_world("mundo001") function. Then we can run all the Karel actions we want, but to see their effect, we have to finish by running the run_actions() function.

Let’s see a first example. We will ask Karel to move forward, pick up an item, and then move forward again:

> # This program makes Karel move one space forward, pick up an item, and then move forward again
> generate_world("mundo001")
> move()
> pick_beeper()
> move()
> run_actions()

A slightly more interesting problem would be for Karel to pick up the item, place it in Street 2 and Avenue 5, and finally move to Street 2 and Avenue 6:

A slightly more interesting problem would be for Karel to pick up the item, place it in Street 2 and Avenue 5, and finally move to Street 2 and Avenue 6:

Now we need Karel to turn right, so that it faces east. However, Karel only knows how to turn left; there is no turn_right() function. Can Karel turn right using the available actions? Yes, it can, by turning left three times to achieve the same effect. Then, it only remains for Karel to move forward, place the item, and move forward again:

> generate_world("mundo001")
> move()
> pick_beeper()
> move()
> turn_left()
> move()
> turn_left()
> turn_left()
> turn_left()
> move()
> move()
> put_beeper()
> move()
> run_actions()

Some examples presented in this tutorial were adapted from “Karel the Robot Learns Java” by Eric Roberts, published in 2005.