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

2016-02-28

### 问题

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等其他语言中一个一个的函数调用是不是酷帅多了？

## 您可能感兴趣的

Higher Kinded Types in Typescript Higher kinded types are not currently possible in typescript but first of all let’s explain why they exist. In functional programming there is the ...
python3 第二十章 – 函数式编程之Higher-order function（高... 什么是高阶函数？把函数作为参数传入，这样的函数称为高阶函数，函数式编程就是指这种高度抽象的编程范式。 在前面的章节中，我们知道可以用abs()这个函数来得到一个数的绝对值，如： print('abs(-100):', abs(-100))以上代码，输出： abs(-10...
python函数式编程之yield表达式形式 先来看一个例子 def foo(): print("starting...") while True: res = yield print("res:",res)g = foo() next(g) 在上面的例子里，因为foo函数中有yie...

Introduction to the suite of JuliaFin packages (pa... As we roll outJuliaPro v0.6.0.1 (now supporting Julia 0.6), we see it as the best time to introduce our old users and new to our productJuliaFin, ...
0