Haskell -Funktion, die leere Liste zurückgibt
-
27-10-2019 - |
Frage
Ich bin wirklich ein absoluter Neuling bei Haskell, also habe ich einen totalen Verlust darüber, wie ich einige Funktionen debuggen, die ich geschrieben habe. Wenn ich anrufe shuntingYard ["3+4"]
Ich komme zurück []
, während ich zurückkommen möchte [34+]
. Jede Hilfe wäre sehr geschätzt.
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 []
Lösung
Als allgemeine Debugging -Technik können Sie das Debug. -TRACE -Modul verwenden, um herauszufinden, welche Funktionen aufgerufen werden und welche Eingaben ihre Eingaben sind. Mit Blick auf den Zustand Ihres Algorithmus nach jedem Schritt.
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 -}
Dies druckt:
(["3+4"],[])
([],[])
Hmm, du hast alles nach dem ersten Anruf verloren. Wenn man sich die Wachen in __shuntingyard ansieht, scheint der Fall "ansonsten" aufgerufen wird.
Vielleicht wolltest du anrufen shuntingYard ["3", "+", "4"]
?
Andere Tipps
Ok, lass uns einfach durchspielen, was passiert, wenn du anrufst shuntingYard ["3+4"]
:
- Es ruft
_shuntingYard ["3+4"] []
- Es geht durch die Wachen von
_shuntingYard
:null stmt = null ["3+4"] = false
all isDigit (head stmt) = all isDigit "3+4" = false
wie+
ist keine ZifferisOperator (head (head stmt)) = isOperator '3' = false
- Auch falsch wie
'3' /= '('
- Auch falsch wie
'3' /= ')'
- Da keiner der Wachen übereinstimmte, gehen wir in den Standardfall und rufen an
_shuntingYard (tail stmt) stack = _shuntingYard [] []
- Diesmal die erste Wache (
null stmt = null []
) Matches, also rufen wir anstackOperations []
und bekomme[]
.