Alright, I'm probably doing this wrong but it has got me pulling my hair out. I haven't been able to find anything to do what I want
Take this pseudocode
my_function left right
= another_function new_left new_right (fourth_function new_left new_right)
where new_left = if some_condition then left else third_function left
new_right = if some_condition then third_function right else right
How can I avoid rechecking some_condition? And I'm not talking about saving some_condition
as another variable in the where
construct. If I put lets
inside the if
I then duplicate the in another_function new_left new_right
.
In an imperative language I could do something like
int new_left;
int new_right;
if (condition) {
new_left = left;
new_right = third_function(right);
} else {
new_left = third_function(left);
new_right = right;
}
return another_function(new_left, new_right, fourth_function(new_left, new_right));
I know in a functional language you're not supposed to think of doing things in a sequence, but rather as a composition of expressions, so I'm just looking for a way to write the original pseudocode such that it's DRY. And it seems like a simple and relatively common case.
Edit
Sorry for the confusion. I can't inline third_function left/right
because I need to use it's value twice (updated pseudocode). And fourth_function
can't be moved inside another_function