为什么在这个 CPS 解析器示例中没有定义 curried 参数?
Why is a curried parameter not defined in this CPS parser example?
代码:
type Result = string option
type Parser<'a> = string -> int -> ('a -> Result) -> ('a -> Result) -> Result
let win r = Some <| "Correct: " + r
let lose _ = None
let parse_a: Parser<char> = fun text i win_cps lose_cps ->
let x = text.[i]
if x = 'a' then win_cps x else lose_cps x
let parse_many: Parser<char> -> Parser<char list> = fun p text i win_cps lose_cps ->
let rec loop: char list -> Parser<char list> = fun l text i _ _ ->
let win = fun (l: char list) (r: char) -> loop (r:l) text i win_cps lose_cps
let lose = fun (l: char list) (r: char) -> win_cps (r:l)
p text (i+1) (win l) (lose l)
loop [] text (i-1) win_cps lose_cps
parse_many parse_a "aaabc" 0 (fun r -> win (r |> System.String.Concat)) lose
错误:cps_parser_v0.fsx(12,59): error FS0039: The type 'l' is not defined
我想在 Haskell 中制作一个功能纯的 CPS 解析器,并首先在 F# 中进行试验。如果我真的想在 F# 中执行此操作,我会使用可变状态,但现在我只是想知道为什么这不起作用?在我看来,它不记得部分应用的参数。
你打错了:(r:l)
应该是 (r::l)
。 :
运算符表示 "is of type",即 r:l
表示您告诉编译器 r
是 l
类型。 ::
运算符表示 "prepend this to the front of this list": r::l
表示 "prepend r
to the front of list l
".
你在两个地方犯了这个错误:loop (r:l)
应该是 loop (r::l)
,再往下一行,win_cps (r:l)
应该是 win_cps (r::l)
.
代码:
type Result = string option
type Parser<'a> = string -> int -> ('a -> Result) -> ('a -> Result) -> Result
let win r = Some <| "Correct: " + r
let lose _ = None
let parse_a: Parser<char> = fun text i win_cps lose_cps ->
let x = text.[i]
if x = 'a' then win_cps x else lose_cps x
let parse_many: Parser<char> -> Parser<char list> = fun p text i win_cps lose_cps ->
let rec loop: char list -> Parser<char list> = fun l text i _ _ ->
let win = fun (l: char list) (r: char) -> loop (r:l) text i win_cps lose_cps
let lose = fun (l: char list) (r: char) -> win_cps (r:l)
p text (i+1) (win l) (lose l)
loop [] text (i-1) win_cps lose_cps
parse_many parse_a "aaabc" 0 (fun r -> win (r |> System.String.Concat)) lose
错误:cps_parser_v0.fsx(12,59): error FS0039: The type 'l' is not defined
我想在 Haskell 中制作一个功能纯的 CPS 解析器,并首先在 F# 中进行试验。如果我真的想在 F# 中执行此操作,我会使用可变状态,但现在我只是想知道为什么这不起作用?在我看来,它不记得部分应用的参数。
你打错了:(r:l)
应该是 (r::l)
。 :
运算符表示 "is of type",即 r:l
表示您告诉编译器 r
是 l
类型。 ::
运算符表示 "prepend this to the front of this list": r::l
表示 "prepend r
to the front of list l
".
你在两个地方犯了这个错误:loop (r:l)
应该是 loop (r::l)
,再往下一行,win_cps (r:l)
应该是 win_cps (r::l)
.