わかりました、私はおそらくこれを間違ってやっていますが、それは私の髪を引っ張っています. やりたいことが見つからない
この擬似コードを取得します
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
some_condition の再チェックを避けるにはどうすればよいですか? some_condition
また、構成内の別の変数として保存することについて話しているのではありませんwhere
。lets
Iの中に入れると、if
を複製しin another_function new_left new_right
ます。
命令型言語では、次のようなことができます
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));
関数型言語では、物事を順番に行うのではなく、式の構成として考える必要があることを知っているので、元の疑似コードを DRY になるように記述する方法を探しています。そして、それは単純で比較的一般的なケースのようです。
編集
混乱させて申し訳ありません。third_function left/right
値を 2 回使用する必要があるため、インライン化できません(更新された疑似コード)。そしてfourth_function
中で動かせないanother_function