F# 函数式编程入门
在 F# 编程中,模式匹配、函数值的使用以及各种操作符的运用是非常重要的部分。下面将详细介绍这些内容。
模式匹配
在 F# 中,模式匹配是一项强大的功能,但使用时需要注意一些问题。
不完全模式匹配
当模式匹配不完全时,F# 编译器会给出警告。例如:
warning FS0025: Incomplete pattern matches on this expression. For example, the value '(_,0)'
may indicate a case not covered by the pattern(s).
val urlFilter3 : url:string -> agent:int -> bool
此时,可能需要添加一个额外的抛出异常的子句来告知编译器某些输入是不期望的:
let urlFilter4 url agent =
match url,agent with
| "http://www.control.org", 86 -> true
| "http://www.kaos.org", _ -> false
| _ -> failwith "unexpected input"
非详尽的匹配会自动添加一个默认情况,在该情况下会抛出 MatchFailureException
超级会员免费看
订阅专栏 解锁全文
1174

被折叠的 条评论
为什么被折叠?



