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 []
War es hilfreich?

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"]:

  1. Es ruft _shuntingYard ["3+4"] []
  2. Es geht durch die Wachen von _shuntingYard:
    1. null stmt = null ["3+4"] = false
    2. all isDigit (head stmt) = all isDigit "3+4" = false wie + ist keine Ziffer
    3. isOperator (head (head stmt)) = isOperator '3' = false
    4. Auch falsch wie '3' /= '('
    5. Auch falsch wie '3' /= ')'
  3. Da keiner der Wachen übereinstimmte, gehen wir in den Standardfall und rufen an _shuntingYard (tail stmt) stack = _shuntingYard [] []
  4. Diesmal die erste Wache (null stmt = null []) Matches, also rufen wir an stackOperations [] und bekomme [].
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top