我在Haskell中编写了一个函数:
recognize :: [GrammarRule] -> State -> [String] -> Bool
recognize [] x [s] = False
recognize gr x [s] = if elem True (map (\state -> elem state (takeSteps gr x [s]))(enders gr)) then True else False
这不是详尽无遗的。识别功能有什么问题? 非常感谢!!
--the function takesteps is here:
takeSteps :: [GrammarRule] -> State -> [String] -> [State]
takeSteps g s [] = [s]
takeSteps g s (w:ws) = concatMap (\nextState -> takeSteps g nextState ws) (successors g s w)
--the function enders is here:
enders :: [GrammarRule] -> [State]
enders [] = []
enders (r:rs) =
let resultFromRest = enders rs in
case r of
End x -> x : resultFromRest
Step x w' y -> resultFromRest
答案 0 :(得分:4)
[s]
匹配仅包含一个元素的列表s
。
如果要匹配任何列表,请使用简单标识符l
,例如:
recognize [] x [s] = False
recognize gr x l = if elem True (map (\state -> elem state (takeSteps gr x l))(enders gr)) then True else False
-- note the usage of l here ^