swarm-0.4: data/scenarios/Tutorials/lambda.yaml
version: 1
name: Lambda
description: |
Learn how to define functions.
objectives:
- goal:
- Your goal in this challenge is to send a robot to grab the
`flower`{=entity} in the lower right (you don't need to bring it back).
- |
The path looks complex, but if you study it, you will see that it
has a lot of structure. In particular, there are many parts of
the path that repeat four times; it seems like it could be really useful to have
a function to repeat a command four times.
- |
To write a function, you use lambda syntax. As a simple example, `\x. x + 1` is the
function which takes an input (locally called `x`{=snippet}) and returns
one more than `x`{=snippet}. As another example:
- |
```
def x4 : cmd unit -> cmd unit = \c. c; c; c; c end
```
- That is, `x4`{=snippet} is defined as the function which takes a command,
called `c`{=snippet}, as input, and returns the command
`c; c; c; c`{=snippet} which consists of executing `c`{=snippet} four times.
condition: |
try {
teleport self (32,-16);
b <- ishere "flower";
return (not b)
} { return false }
solution: |
def x4 = \c. c; c; c; c end;
def m2 = move; move end;
def m4 = x4 move end;
def tR = turn right end;
def tL = turn left end;
def s = m4; tL; m2; tL; m4; tR; m2; tR end;
build {
turn right;
x4 (x4 s; m4; m2; tR; x4 (x4 move); tL; m2);
grab
}
robots:
- name: base
dir: [0,1]
display:
char: Ω
attr: robot
devices:
- logger
- 3D printer
- dictionary
inventory:
- [10, logger]
- [10, compass]
- [10, treads]
- [10, solar panel]
- [10, grabber]
- [10, lambda]
- [0, boulder]
- [0, flower]
world:
palette:
'Ω': [grass, null, base]
'.': [grass]
'*': [grass, flower]
'@': [grass, boulder]
'┌': [stone, upper left corner]
'┐': [stone, upper right corner]
'└': [stone, lower left corner]
'┘': [stone, lower right corner]
'─': [stone, horizontal wall]
'│': [stone, vertical wall]
upperleft: [-1,1]
map: |
┌─────────────────────────────────┐
│.......@.......@.......@.......@@│
│.@@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@@│
│.....@.@.....@.@.....@.@.....@.@@│
│@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@.@@│
│.....@.@.....@.@.....@.@.....@.@@│
│.@@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@@│
│.....@.@.....@.@.....@.@.....@.@@│
│@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@.@@│
│.....@.@.....@.@.....@.@.....@.@@│
│.@@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@@│
│.....@.@.....@.@.....@.@.....@.@@│
│@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@.@@│
│.....@.@.....@.@.....@.@.....@.@@│
│.@@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@@│
│.....@.@.....@.@.....@.@.....@.@@│
│@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@.@@│
│Ω....@.......@.......@.......@..*│
└─────────────────────────────────┘