Sam*_*uel 5 f# functional-programming
编写以下内容的F#惯用方法是什么?或者你会原样离开吗?
let input = 5
let result =
if input > 0 && input < 5 then
let a = CalculateA(input)
let b = CalculateB(input)
(a+b)/2
else
CalculateC(input)
Run Code Online (Sandbox Code Playgroud)
Rob*_*ert 10
对于其中一个,if ... then ... else ...我可能会这样离开,如果你有更多的情况我会使用模式匹配与守卫:
let result =
match input with
| _ when input > 0 && input < 5 -> ...
| _ -> ...
Run Code Online (Sandbox Code Playgroud)
或者您可能还想查看活动模式:http://msdn.microsoft.com/en-us/library/dd233248.aspx
| 归档时间: |
|
| 查看次数: |
582 次 |
| 最近记录: |