f# - 将嵌套匹配表达式转换为函数
问题描述
我有一些嵌套的歧视联合
type Job = Sniff | Guard
type Dog = Chihuahua | GermanShepherd of Job
这是一个接受 aDog
并返回 a的函数string
。
let dogPrinter d =
match d with
| Chihuahua -> ""
| GermanShepherd g ->
match g with
| Sniff -> ""
| Guard -> ""
我可以将第一个转换match
为function
语法:
let dogPrinter = function
| Chihuahua -> ""
| GermanShepherd g ->
match g with
| Sniff -> ""
| Guard -> ""
如何将第二个转换match
为function
?
解决方案
在这种情况下避免嵌套匹配的惯用方法是使用嵌套模式:
let dogPrinter = function
| Chihuahua -> ""
| GermanShepherd Sniff -> ""
| GermanShepherd Guard -> ""
您可以根据需要尽可能深地嵌套模式,就像在创建值时可以嵌套表达式一样。
推荐阅读
- c# - 创建具有可调节线宽的箭头 ObjectARX C#
- python - 使用 Python 自动导出 DOORS 模块
- sql - DATE 和 VARCHAR 之间的比较:它是如何工作的?
- python - tf.math.pow(x, 0.5) 的激活函数导致 NaN 损失
- java - 创建一个可以分配模板字符串中给定变量的函数
- python - 当我在执行过程中尝试退出窗口或垃圾邮件单击窗口时,Pygame 窗口没有响应
- pandas - 在 Pandas 中交换列值
- express - TypeError:无法读取 Express 中未定义的属性“应用”
- python - 如何使用era5数据绘制风平均值?
- r - 用于引导的替换抽样 [R]