我想检查前一个的条件if condition
以确定下一个if condition
是否要执行。每个都if condition
可能返回一个值。
编辑:抱歉,我之前提供的示例看起来有点奇怪......:(这是我的真实示例,我想简化if-then-else
for goingToMove
goingToMove p routes points w h =
if canMove p points
-- the point can be moved in the map
then let r = routes ++ [p]
l = remainList p points
in move p r l w h
-- the point cannot be moved in the maps
else []
move p routes points w h =
if (length routes) == 2
then routes
else let one = goingToMove (tallRightCorner p) routes points w h in
if (null one)
then let two = goingToMove(tallRightBCorner p) routes points w h in
if (null two)
then let three = goingToMove (tallLeftBCorner p ) routes points w h in
if (null three)
then ....
...... -- until, let eight = ..
else three
else two
else one
编辑:坏例子 当这个东西是用java写的,我可能会使用一个可变的布尔标志,并返回一个可变的数据。
public String move (int number){
// base case
if (number == 0){
return "Finished the recursion";
}
// general case
else {
String result;
boolean isNull = false;
if ((result = move(3)) == null){
isNull = true;
}
else {
return result;
}
// continue to execute the if-conditions if the previous condition failed
if (isNull){
if((result = move(2)) == null){
isNull = true;
}
else {
return result;
}
}
if (isNull){
if((result = move(1)) == null){
isNull = true;
}
else {
return result;
}
}
return null;
}
}
但是在 Haskell 中,没有可变数据,只有if-then-else
条件。然后代码看起来像这样,我想简化一下,因为在我的实际工作中,有 8 个级别if-then-else
看起来很糟糕和混乱......
move 0 = "Finished the recursion"
move n =
let one = move 3 in
if null one
then let two = move 2 in
if null two
then let three = move 1 in
then null
else three
else two
else one