Función Haskell que regresa la lista vacía
-
27-10-2019 - |
Pregunta
Realmente soy un novato absoluto en Haskell, así que estoy en una pérdida total sobre cómo depurar algunas funciones que escribí. Cuando llamo shuntingYard ["3+4"]
Vuelvo []
, mientras que quiero volver [34+]
. Cualquier ayuda sería muy apreciada, muy apreciada.
import Char
isOperator :: Char -> Bool
isOperator x = elem x ['+','-','*','/','%','^','!','=','<','>']
associativityOf :: Char -> String
associativityOf x = if elem x ['+','-','*','/','%']
then "Left"
else "Right"
precedenceOf :: Char -> Int
precedenceOf x
| elem x "=<>" = 1
| elem x "+-" = 2
| elem x "*/%" = 3
| elem x "^!" = 4
| otherwise = 0
operatorActions :: [[Char]] -> [[Char]] -> [[Char]]
operatorActions stmt stack
| ( tokenAssoc == "Left" && tokenPrecedence <= stackPrecedence ) ||
( tokenAssoc == "Right" && tokenPrecedence < stackPrecedence ) =
[stackOper] : _shuntingYard stmt (tail stack)
| otherwise = _shuntingYard (tail stmt) ((head stmt) : stack)
where tokenAssoc = associativityOf (head (head stmt))
tokenPrecedence = precedenceOf (head (head stmt))
stackOper = if (not (null stack))
then (head (head stack))
else '='
stackPrecedence = precedenceOf stackOper
stackOperations :: [[Char]] -> [[Char]]
stackOperations stack
| ((not (null stack)) && (head (head stack)) == '(') =
error "Unbalanced parens."
| null stack = []
| otherwise = (head stack) : _shuntingYard [] (tail stack)
_shuntingYard :: [[Char]] -> [[Char]] -> [[Char]]
_shuntingYard stmt stack
| null stmt = stackOperations stack
| all isDigit (head stmt) = (head stmt) : _shuntingYard (tail stmt) stack
| isOperator (head (head stmt)) = operatorActions stmt stack
| (head (head stmt)) == '('=
_shuntingYard (tail stmt) ((head stmt) : stack)
| (head (head stmt)) == ')' = if (head (head stack)) == '('
then _shuntingYard (tail stmt) (tail stack)
else (head stack) : _shuntingYard stmt (tail stack)
| otherwise = _shuntingYard (tail stmt) stack
shuntingYard :: [[Char]] -> [[Char]]
shuntingYard stmt = _shuntingYard stmt []
Solución
Como técnica de depuración general, puede usar el módulo de transmisión de depuración. Para averiguar qué funciones se llaman y cuáles son sus entradas. Usando mirar el estado de su algoritmo después de cada paso.
import Debug.Trace
-- Show arguments each time _shuntingYard is called
_shuntingYard :: [[Char]] -> [[Char]] -> [[Char]]
_shuntingYard stmt stack = traceShow (stmt, stack) $ __shuntingYard stmt stack
__shuntingYard stmt stack
| null stmt = stackOperations stack
{- etcetera -}
Esto imprime:
(["3+4"],[])
([],[])
Hmm, perdiste todo después de la primera llamada. Mirando a los guardias en __shustingyard, parece que se llama al caso "de lo contrario".
Quizás quisieras llamar shuntingYard ["3", "+", "4"]
?
Otros consejos
Ok, jugemos a través de lo que sucede cuando llamas shuntingYard ["3+4"]
:
- Llama
_shuntingYard ["3+4"] []
- Atraviesa los guardias de
_shuntingYard
:null stmt = null ["3+4"] = false
all isDigit (head stmt) = all isDigit "3+4" = false
como+
no es un dígitoisOperator (head (head stmt)) = isOperator '3' = false
- También falso como
'3' /= '('
- También falso como
'3' /= ')'
- Dado que ninguno de los guardias coincidió, por lo que entramos en el caso predeterminado y llamamos
_shuntingYard (tail stmt) stack = _shuntingYard [] []
- Esta vez el primer guardia (
null stmt = null []
) coinciden, por lo que llamamosstackOperations []
y obten[]
.