给定一个受歧视工会的案例,你能得到下一个案例吗?

Ale*_*x_P 3 f# types

我正在尝试为机器人模拟编写代码。对于方向,我有一个 DU:

type Direction = North | East | South | West
Run Code Online (Sandbox Code Playgroud)

当类型为South且您给出向右转两次的指令时,是否有一个选项使该值实际上再次从 开始North

小智 8

你所拥有的是一个定义了 4 种情况的受歧视联盟。没有自动方法来理解旋转值,但您可以编写自己的函数来实现此目的:

type Direction = North | East | South | West

module Direction = 
    let turnRight = 
        function  //The function keyword is a shortcut for (fun x -> match x with...)
        | North -> East
        | East -> South
        | South -> West
        | West -> North

//Usage
let direction = North
let direction2 = Direction.turnRight direction
Run Code Online (Sandbox Code Playgroud)

注意:在 F# 中,通常会在同名模块中声明作用于特定类型的函数。