問題,我有以下ADT:與應用型風格秒差距
type Program = [Expr]
data Expr =
Num Int
| Bool Bool
| Binding String Expr
deriving (Show)
這裏有一個變量綁定表達式形式lhs is rhs
的,解析器。
binding :: Parser Expr
binding = do
lhs <- word
spaces
string "is"
spaces
rhs <- expr
return $ Binding lhs rhs
它工作正常,但是當我嘗試將其轉換爲應用樣式時,它會給出錯誤的結果。
binding :: Parser Expr
binding = Binding <$> word <* (spaces *> string "is" *> spaces) *> expr
在parenthesised部分與>>
更換*>
也不能工作。這兩個實現有什麼區別?是否有組合兩個解析器並忽略兩者的結果?
試圖用Debug.trace
進行調試也沒有效果......沒有打印任何內容。
binding :: Parser Expr
binding = (\x y -> trace (show (x, y)) (Binding x y)) <$> word <* (spaces *> string "is" *> spaces) *> expr
解析器的其餘部分,上下文:
word :: Parser String
word = many1 letter
expr :: Parser Expr
expr = binding <|> atom
program :: Parser Program
program = do
spaces
result <- many (expr <* spaces)
return result
快速猜測,但是不應該在'binding'定義中的'word'之後使用'(<*>)'? I.e:'binding = Binding <$> word <*>(spaces *> string「is」*> spaces)*> expr' – danem 2015-02-23 04:44:41
這是行不通的?也許你應該提供更多的代碼。 http://lpaste.net/121008 – danem 2015-02-23 05:10:50
你很快就發現了。:) 謝謝您的幫助! – user1953221 2015-02-24 03:02:42