Desidero verificare le condizioni del precedente if condition
per determinare se il prossimo if condition
deve essere eseguito o meno. Ogni if condition
può restituire un valore.Come semplificare nidificato-se si utilizza per restituire il valore in Haskell
Edit: Ci scusiamo per che l'esempio che ho fornito prima di guardare un po 'strano ... :( Questo è il mio vero esempio, e voglio semplificare il if-then-else
per 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
Edit: cattivo esempio Quando questa cosa è scritto in Java, che può utilizzare un flag booleano mutevole, e restituire un dato mutabili.
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;
}
}
Ma in Haskell, non ci sono dati mutabili e solo la condizione if-then-else
. Poi il codice sarà simile a questo, e voglio semplificare questo perché nel mio vero lavoro, ci sono 8 livelli di if-then-else
che sembrano terribile e disordinato ....
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
Il 'move' che stai chiamando nel tuo codice Java è lo stesso del' move' che stai definendo? Se è così, non vedo come non si elimini all'infinito per un input diverso da zero. 'move (3)' chiama 'move (3)' chiama 'move (3)' ... – rampion
Anche il codice haskell che hai fornito ha il tipo sbagliato. A un certo punto nel codice java stai restituendo "null", il che significa che il codice haskell dovrebbe essere "Int -> Forse String", anche se non era un ciclo infinito. Oh, e il codice haskell che suggerisci ha anche un errore di sintassi (manca una riga con un if in esso?) Che rende difficile capire esattamente cosa stai facendo. – Carl
Ho fornito un esempio reale :(mi dispiace – code4j