gpt4 book ai didi

f# - 如何将 FParsec 结果值提取到 FSI 中的变量

转载 作者:行者123 更新时间:2023-12-05 01:18:14 24 4
gpt4 key购买 nike

我正在使用 FParsec 并尝试将结果值绑定(bind)到 FSI 中的变量。我尝试了以下内容:

        > run pint32 "3";; // succeeds
val it : ParserResult<int32,unit> = Success: 3
> let x = run pint32 "3";;
val x : ParserResult<int32,unit> = Success: 3
> x;; // I bind the result to x
val it : ParserResult<int32,unit> = Success: 3
> let Success(y, _, _) = x;; //It looks like I can extract the value...
val Success : y:'a * 'b * 'c -> ParserResult<int32,unit>
> y;;
...error FS0039: The value or constructor 'y' is not defined

它似乎绑定(bind)了然后忘记了它,但我想我遗漏了一些东西,因为以下解构有效:

> type AB = A of int | B
let aa = A 1
let A a = aa;;

type AB =
| A of int
| B
val aa : AB = A 1
val A : a:'a -> AB
> a;;
val it : int = 1

下面的函数似乎能够使用匹配语句提取值(尽管我需要根据解析器更改失败类型):

> let extract p str =
match run p str with
| Success(result, _, _) -> result
| Failure(errorMsg, _, _) -> 3
let z = extract pint32 "3"
z;;

val extract : p:Parser<int,unit> -> str:string -> int
val z : int = 3
val it : int = 3

我误会了什么?

最佳答案

您遇到了与最近这个问题相同的问题:

F#: Destructuring bind with a discriminated union

您的 let Success(y, _, _) = x 行实际上创建了一个名为 Success 的函数,该函数采用单个三元组作为参数。你需要做的是 let (Success(y, _, _)) = x

关于f# - 如何将 FParsec 结果值提取到 FSI 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36264932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com