Funzione Haskell che restituisce un elenco vuoto
-
27-10-2019 - |
Domanda
Sono davvero un principiante assoluto a Haskell, quindi sono in perdita totale su come eseguire il debug di alcune funzioni che ho scritto. Quando chiamo shuntingYard ["3+4"]
torno []
, mentre voglio tornare [34+]
. Qualsiasi aiuto sarebbe molto, molto apprezzato.
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 []
Soluzione
Come tecnica di debug generale, è possibile utilizzare il modulo Debug.Trace per scoprire quali funzioni vengono chiamate e quali sono i loro input. Usando lo stato del tuo algoritmo dopo ogni passaggio.
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 -}
Questa stampe:
(["3+4"],[])
([],[])
Hmm, hai perso tutto dopo la prima chiamata. Guardando le guardie in __shuntingyard, sembra che il caso "altrimenti" venga chiamato.
Forse volevi chiamare shuntingYard ["3", "+", "4"]
?
Altri suggerimenti
Ok, giochiamo solo attraverso quello che succede quando chiami shuntingYard ["3+4"]
:
- Chiama
_shuntingYard ["3+4"] []
- Passa attraverso le guardie di
_shuntingYard
:null stmt = null ["3+4"] = false
all isDigit (head stmt) = all isDigit "3+4" = false
come+
non è una cifraisOperator (head (head stmt)) = isOperator '3' = false
- Anche falso come
'3' /= '('
- Anche falso come
'3' /= ')'
- Dal momento che nessuna delle guardie corrisponde, quindi andiamo nel caso predefinito e chiamiamo
_shuntingYard (tail stmt) stack = _shuntingYard [] []
- Questa volta la prima guardia (
null stmt = null []
) corrispondenze, quindi chiamiamostackOperations []
e prendi[]
.