【发布时间】:2019-07-08 01:37:35
【问题描述】:
我有以下自定义数据类型:
type Length = Integer
type Rotation = Integer
data Colour = Colour { red, green, blue, alpha :: Int }
deriving (Show, Eq)
data Special
= L Length
| R Rotation
| Col Colour
deriving (Show, Eq)
假设我有一个如下形式的元组:
let x = ("Jump", R 90)
我使用以下方法提取元组中的第二个值:
snd x = R 90
有什么方法可以使用模式匹配从 R 90 中获取 Rotation 值 90,以便我可以在另一个代码区域中使用它?当我使用snd x 时,结果的类型是 Special 类型,但我只想获取 Rotation 值。任何见解都值得赞赏。
【问题讨论】:
-
case snd x of R n -> ...? -
let (R rotation) = snd x in ...
标签: haskell functional-programming pattern-matching