Chapter 5 | To index | Haski the Robot |
Basic Robot commands
go_forward | if not blocked by a wall the robot moves one fiels in the direction it is currently facing |
turn_right | the robot turns right 90 degrees |
take_item | the robot takes up an item from the current field, if there is one |
drop_item | the robot drops an item on the current field, if he is carrying one |
do_nothing | guess what ;) |
Boolean sensors of a robot
front_free | returns False if the robot is facing a wall, True otherwise |
right_free | returns False if there's a wall to the robot's right side, True otherwise |
left_free | returns False if there's a wall to the robot's left side, True otherwise |
field_has_item | returns True if there is at least one item on the field the robot is currently located on, False otherwise |
is_carrying | returns True if the robot is currently carrying at least one item, False otherwise |
facing_up | returns True if the robot is currently facing up, False otherwise |
facing_right | returns True if the robot is currently facing right, False otherwise |
facing_down | returns True if the robot is currently facing down, False otherwise |
facing_left | you should have got it by now ;) |
Combining sensors into conditions
Any sensor on its own is already a condition. Now assume that p and q are conditions
p :& q | returns True only if both p and q are True; returns False as soon as one of them is False |
p :| q | returns False only if both p and q are False; returns True as soon as one of them is True |
Not p | returns True only if p is False, returns False if p is True |
The usual bracketing rules apply, i.e. Not has the highest precedence (i.e. it binds most tightly) whereas :& has the lowest precedence. Everything else has to be put in brackets appropriately.
Conditional expressions
Assume p to be a condition and c and d a command or a (bracketed) sequence
IfThen p c | executes the command(s) c only if p returns True, otherwise does nothing |
IfThenElse p c d | executes the command(s) c only if p returns True, otherwise executes d |
While p c | repeats c as long as p returns True, checking p for the first time before the initial execution |
DoWhile c p | repeats c as long as p returns True, but executes c once and then checks p. Equivalent to c &> While p c. |
Functions
A command or a sequence of commands can be assigned a name, making it a function:
name = command1 [ &> command2 [ &> ...] ]
This function can then be called anywhere inside the same Haski-program. Function names can consist of small and capital letters, digits, hyphens - and underscores _.
Every Haski program needs to have at least one function, namely main; this is the function the robot is initialised with when placing it on the working area.
Comments
If you, for example, want to keep some explanations of what your code does in the actual program file, you can enter two hyphens "--", everything succeeding these, until the end of the current line, will be ignored.
by Lars Otten, 2004 | ![]() |