I have a C# method that looks a bit like this:
bool Eval() {
// do some work
if (conditionA) {
// do some work
if (conditionB) {
// do some work
if (conditionC) {
// do some work
return true;
}
}
}
return false;
}
In F# this ends up looking quite a bit uglier because of the mandatory else branches:
let eval() =
// do some work
if conditionA then
// do some work
if conditionB then
// do some work
if conditionC then
// do some work
true
else
false
else
false
else
false
What would be a cleaner way of writing this in F#?
Well, since ‘do some work’ is already imperative (presumably), then I think that
is shorter and reasonable. (In other words,
elseis only mandatory forifexpressions that return values; since you’re doing imperative work, useifstatements that don’t need anelse.)