Haskell函数返回空列表

时间:2011-03-20 18:58:52

标签: haskell shunting-yard

我真的是Haskell的绝对新手,所以我完全不知道如何调试我写的一些函数。当我致电shuntingYard ["3+4"]时,我会回来[],而我想回来[34+]。任何和所有的帮助都将受到极大的赞赏。

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 []

2 个答案:

答案 0 :(得分:5)

作为一种常规调试技术,您可以使用Debug.Trace模块找出正在调用的函数以及它们的输入内容。使用每个步骤后查看算法的状态。

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 -}

打印:

(["3+4"],[])
([],[])
嗯,你在第一次通话后失去了一切。看着__shuntingYard中的守卫,看来“其他”案件被调用了。

也许您想致电shuntingYard ["3", "+", "4"]

答案 1 :(得分:3)

好的,让我们来看看你致电shuntingYard ["3+4"]时发生的事情:

  1. 调用_shuntingYard ["3+4"] []
  2. 它通过了_shuntingYard的守卫:
    1. null stmt = null ["3+4"] = false
    2. all isDigit (head stmt) = all isDigit "3+4" = false由于+不是数字
    3. isOperator (head (head stmt)) = isOperator '3' = false
    4. 同样为'3' /= '('
    5. 同样为'3' /= ')'
  3. 由于没有一名警卫匹配,所以我们进入默认情况并致电_shuntingYard (tail stmt) stack = _shuntingYard [] []
  4. 这次第一个警卫(null stmt = null [])匹配,因此我们致电stackOperations []并获取[]