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 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: in general, `\x. blah` is the function which takes an input (locally called `x`) and returns `blah` as its output (`blah` can of course refer to `x`). For example: - | def x4 : cmd unit -> cmd unit = \c. c; c; c; c end - That is, `x4` is defined as the function which takes a command, called `c`, as input, and returns the command `c; c; c; c` which consists of executing `c` 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 { require "treads"; require "lambda"; // #540 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: default: [blank] 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: | ┌─────────────────────────────────┐ │.......@.......@.......@.......@@│ │.@@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@@│ │.....@.@.....@.@.....@.@.....@.@@│ │@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@.@@│ │.....@.@.....@.@.....@.@.....@.@@│ │.@@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@@│ │.....@.@.....@.@.....@.@.....@.@@│ │@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@.@@│ │.....@.@.....@.@.....@.@.....@.@@│ │.@@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@@│ │.....@.@.....@.@.....@.@.....@.@@│ │@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@.@@│ │.....@.@.....@.@.....@.@.....@.@@│ │.@@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@@│ │.....@.@.....@.@.....@.@.....@.@@│ │@@@@.@.@@@@@.@.@@@@@.@.@@@@@.@.@@│ │Ω....@.......@.......@.......@..*│ └─────────────────────────────────┘