编写以下内容的 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)
最佳答案
对于一个 if ... then ... else ...
我可能会这样,如果你有更多的情况,我要么使用模式匹配和 when 守卫:
let result =
match input with
| _ when input > 0 && input < 5 -> ...
| _ -> ...
或者您可能还想查看事件模式:http://msdn.microsoft.com/en-us/library/dd233248.aspx
关于f# - if .. else .. 是一种用 F# 写东西的惯用方式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9016041/