# 硝烟中的函数式编程 – 一个例子

### 问题

A squad of robotic rovers are to be landed by NASA on a plateau on Mars. This plateau, which is curiously rectangular, must be navigated by the rovers so that their on-board cameras can get a complete view of the surrounding terrain to send back to Earth. A rover’s position and location is represented by a combination of x and y co-ordinates and a letter representing one of the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner and facing North. In order to control a rover, NASA sends a simple string of letters. The possible letters are ‘L’, ‘R’ and ’M’. ‘L’ and ‘R’ makes the rover spin 90 degrees left or right respectively, without moving from its current spot. ’M’ means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+1).

### 求解

``````-- 北，东，南，西
data Direction = N | E | S | W
deriving (Show)

-- 左转，右转，和移动
data Command = L | R | M
deriving (Show)

-- 位置
type Point = (Int, Int)

-- 探测器
data MarsRover = MarsRover Point Direction
deriving (Show)
``````

``````turnLeft :: MarsRover -> MarsRover
turnLeft rover = case rover of
MarsRover p N -> MarsRover p W
MarsRover p W -> MarsRover p S
MarsRover p S -> MarsRover p E
MarsRover p E -> MarsRover p N

turnRight :: MarsRover -> MarsRover
turnRight rover =
case rover of
MarsRover p N -> MarsRover p E
MarsRover p W -> MarsRover p N
MarsRover p S -> MarsRover p W
MarsRover p E -> MarsRover p S

move :: MarsRover -> MarsRover
move rover =
case rover of
MarsRover (x, y) N -> MarsRover (x, y+1) N
MarsRover (x, y) E -> MarsRover (x+1, y) E
MarsRover (x, y) W -> MarsRover (x-1, y) W
MarsRover (x, y) S -> MarsRover (x, y-1) S
``````

，函数中使用了 模式匹配

``````execute :: MarsRover -> Command -> MarsRover
execute rover command =
case command of
L -> turnLeft rover
R -> turnRight rover
M -> move rover
``````

``````main :: IO()
main = do
putStrLn \$ show \$ move.turnLeft.move.turnRight.move \$ MarsRover (0, 0) S
-- 输出结果: MarsRover (-1,-2) S
``````

，然后执行了下列操作：

1. 前移
2. 右转
3. 前移
4. 左转
5. 前移

• 定义了基本几个 数据类型
• 定义一系列 纯函数
• 使用了 模式匹配
来实现代码功能。
• 在最后验证的部分，我们利用了函数式编程中 高阶函数
函数(.)把对探测器的多步操作组合为一个函数。 比起Java等其他语言中一个一个的函数调用是不是酷帅多了？

|