|Chapter 1||To index||
Haski the Robot
What is it all about?
Haski is supposed to offer the user a simple and fun way to learn some basic principles of programming. To accomplish this a simplistic programming language was created which provides only a few commands and very basic features. Thereby the whole thing is kept as simple as possible while still putting out the underlying ideas.
What is needed?
Everything needed is included in the archive at the Haski-homepage. There are precompiled versions for Linux (x86, wxHaskell library is needed in addition) and Windows, in addition to that the Haskell-sourcecode is available under the GPL (which could e.g. be used by course instructors to incorporate their own modifications).
Let's look at the most important files inside the archive:
We will see how to actually start your Haski-program later, let's first get into some programming -- finally! :-)
A first example
Let's look at a simple working area:
The working area is divided into 20x15 fields. A red line between two fields stands for a wall which can't be crossed. The arrow in the middle represents the actual robot, its direction singals where the robot is currently facing. A number of 'blue dots' on a field means that the respective amount of items (e.g. crates with goods) is lying on that field, where anything above 9 items is no longer explicitly shown.
Haski, i.e. our robot, can walk around the working area (as long as there are no walls in its way, of course), pick up and drop items. Now let's assume that in this simple example we want our robot to collect all items. We would want him to go to the stack with two items first (since it's the nearest), pick those up, go to the other stack and pick this item up as well. How can we do that?
Haski, as a robot, understands only some simple commands with which you can tell him what to do on the working area. Let's start with two very basic ones, go_forward and turn_right. As the name suggests those make the robot go one field in the direction it's currently facing (if no wall prohibits that) and turn right 90 degrees, respectively. To make Haski pick up a item we use the command take_item. Then we just need to know how to combine two or more commands to make the robot execute them in sequence: This can be done via &>, a so called operator.
Well, that's all we need -- let's make Haski walk to the first stack of items:
turn_right &> go_forward &> go_forward &> go_forward
Then we should have him pick up two items, walk to the second stack and pick that one up as well. This is pretty straightforward, so here's the code:
main = turn_right &> go_forward &> go_forward &> go_forward &> take_item &> take_item &> turn_right &> go_forward &> go_forward &> take_item
Please note that we have to prepend main = to the program -- I'll explain later why this is needed.
Run the program
So now let's see if all that really works. First we would have to put the above code into a text-file, but I already did that for you. :-) It's called example1.hs and can be found in the programs-subfolder; go ahead and take a look, you will notice that you can distribute the commands over several lines in the file.
Now we start the program by executing haski example1 example1 at a command prompt inside the main Haski-directory (under Linux it might be ./haski example1 example1)-- the general command is haski <mapname> <programname>. A window should come up and you should be able to test your little program.
A first extension
To make things a little more interesting we now want to deposit all the items we just collected in the lower right corner of the little rectangle the robot is currently working in. To do this we need the command drop_item, which has Haski drop one item (if he is carrying one). Using this we extend our program as follows:
main = turn_right &> go_forward &> go_forward &> go_forward &> take_item &> take_item &> turn_right &> go_forward &> go_forward &> take_item &> go_forward &> turn_right &> turn_right &> turn_right &> go_forward &> drop_item &> drop_item &> drop_item
Perhaps you noticed that in order to make the robot turn left 90 degrees, we had to tell it to to turn right three times -- Some people claim that the people constructing Haski were running out of money and therefore couldn't afford to implement a command for turning left. ;-) Anyway, this is obvioulsy quite awkward, so what can be done about that? Luckily the Haski-language provides the possibility to combine several commands and give a new name to that combination, a so-called function-definition. Let's see how this works:
main = turn_right &> go_forward &>
go_forward &> go_forward &> take_item &> take_item &>
turn_right &> go_forward &> go_forward &> take_item &>
go_forward &> turn_left &> go_forward
&> drop_item &> drop_item &> drop_item
turn_left = turn_right &> turn_right &> turn_right
This makes sense, doesn't it? We define a new function turn_left, which makes the robot turn right three times (effectively turning left); we can then call that function at other places in our program (underlined above).
Functions and Programs
This is probably also a good time to explain why we had to put that main = in front of our program before: By that we defined a function main, which is always the function the robot is started with. From an more abstract point of view we can therefore state that every program consists of a number of functions and that one of them should be called "main", because this will be called by the robot when he starts executing the program.
So far, so good: Let's go for some conditional expressions in chapter 2 !
|by Lars Otten, 2004|