【发布时间】:2015-09-15 11:47:46
【问题描述】:
我正在尝试将有关 monadic 解析器 (https://www.cs.nott.ac.uk/~gmh/pearl.pdf) 的示例翻译成 F#。
到目前为止我有:
type Parser<'a> = Parser of (string -> ('a*string) list)
let item : Parser<char> =
Parser (fun (s:string) ->
match s with
| "" -> []
| null -> []
| _ -> (s.Chars(0),s.Substring 1)::[])
let sat (pred : (char -> bool)) : Parser<char> =
parserWf
{
let! c = item
if pred c then return c
}
let char c : Parser<char> =
sat (fun c' -> c'.Equals(c))
let rec string (str:string) : Parser<string> =
parserWf
{
if (str.Length > 0)
then
let! c = char (str.Chars 0)
let! cs = string (str.Substring 1)
printfn "String: %s" cs
return c.ToString() + cs
else
return ""
}
如果我从string 方法中删除else return "",那么结果总是空列表。
在 Haskell 中声明了字符串函数:
string :: String -> Parser String
string "" = return ""
string (c:cs) = do {char c; string cs; return (c:cs)}
这很好用。 为什么 F# 函数没有按预期工作?
【问题讨论】:
-
您应该展开“不起作用”。类型错误?运行时错误?结果错误?
-
“为什么 F# 函数没有按预期工作 [当我删除
else return ""]?”嗯......这很大程度上取决于你的期望。也许你应该说那是什么。 -
如果我删除字符串函数的最后两行(否则返回“”)。然后,如果我执行 'runParser (string "hello") "hello!",在 F# 中我得到空列表,但在 Haskell 中我得到 [("hello","!")]。