F# 中的 String.split 问题

发布于 2024-12-06 07:44:45 字数 926 浏览 0 评论 0原文

我在使用以下代码时遇到问题,我正在尝试构建一个词法分析器。

我再次使用 F# for Scientifics 中的示例。

    let lines_of_file filename =
       seq { use stream = File.OpenRead filename
             use reader = new StreamReader(stream)
             while not reader.EndOfStream do
             yield reader.ReadLine() };;

    let read_matrix filename =
      lines_of_file filename
      |> Seq.map (String.split [' '])
      |> Seq.map (Seq.map float)
      |> Math.Matrix.of_seq;;

我声明了以下命名空间:-

          open System
          open System.IO
          open System.Runtime.Serialization.Formatters.Binary
          open Microsoft.FSharp.Core

但在 read_matrix 函数中,“Split.string”中的“split”未被识别。此外,智能感知无法识别“矩阵”。

我尝试声明很多名称空间来查看它们是否识别该方法,但没有任何效果(我的智能感知甚至无法识别 System.Math)。

如果这是一个愚蠢的问题,我深表歉意,我查遍了 MSDN 和其他地方,但我找不到任何东西。

谁能帮助我让VS识别“split”和“Matrix”?

非常感谢。

I am having trouble with the following code, I am trying to build a lexer.

Again I am using the examples from F# for Scientists.

    let lines_of_file filename =
       seq { use stream = File.OpenRead filename
             use reader = new StreamReader(stream)
             while not reader.EndOfStream do
             yield reader.ReadLine() };;

    let read_matrix filename =
      lines_of_file filename
      |> Seq.map (String.split [' '])
      |> Seq.map (Seq.map float)
      |> Math.Matrix.of_seq;;

I have the following namespaces declared:-

          open System
          open System.IO
          open System.Runtime.Serialization.Formatters.Binary
          open Microsoft.FSharp.Core

But in the read_matrix function the "split" in "Split.string" is not recognised. Also the intellisense does not recognise "Matrix".

I have tried declaring a lot of namespaces to see if they recognise the method, but nothing works (my intellisense does not even recognise System.Math).

I apologise if this is a stupid question, I have looked all over MSDN and elsewhere but I could not find anything.

Can anyone help me to get VS to recognise "split" and "Matrix"?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请帮我爱他 2024-12-13 07:44:47

有一些问题。你的外壳错了。它是分割,不是分割。这是一个实例(非静态)方法。分隔符必须是数组,而不是列表。以下工作:

let read_matrix filename =
  lines_of_file filename
  |> Seq.map (fun line -> line.Split ' ')
  |> Seq.map (Seq.map float)
  |> Math.Matrix.ofSeq

顺便说一句,Math.Matrix.of_seq 已被弃用。现在是Math.Matrix.ofSeq。

There are a few problems. Your casing is wrong. It's Split, not split. It's an instance (not static) method. The separators must be an array, not list. The following works:

let read_matrix filename =
  lines_of_file filename
  |> Seq.map (fun line -> line.Split ' ')
  |> Seq.map (Seq.map float)
  |> Math.Matrix.ofSeq

Incidentally, Math.Matrix.of_seq has been deprecated. It is now Math.Matrix.ofSeq.

笑忘罢 2024-12-13 07:44:47

为了使实例方法在 F# 中使用起来更自然,常见的做法是定义简单的辅助函数:

let split separators (x:string) = x.Split(separators)
// Can now pipe into it:
lines_of_file filename
|> Seq.map (split [|' '|])

A common thing to do to make instance methods more natural to use in F# is to define simple helper functions:

let split separators (x:string) = x.Split(separators)
// Can now pipe into it:
lines_of_file filename
|> Seq.map (split [|' '|])
森林很绿却致人迷途 2024-12-13 07:44:47

另外对于 Split 问题.. Split 是字符串实例上的方法.. 您不能像以前那样将其称为静态方法..

例如: "Some string value".Split([|' '|] )
是分割字符串并将分隔符列表作为数组传递的正确方法

Also for the Split issue.. Split is a method on a string instance.. You can not call it as a static method as you did..

ex: "Some string value".Split([|' '|])
is a correct approach to split a string passing the list of delimiters as an array

吲‖鸣 2024-12-13 07:44:47

至于 Math.Matrix 方法,它来自您需要安装的 f# powerpack...

您可以阅读此 Stackoverflow 线程。
发生了什么

另外对于 Split 问题.. Split 是字符串实例上的方法.. 您不能像您那样将其称为静态方法..

例如:“Some string value”.Split([|' '|]) 是分割字符串的正确方法,将分隔符列表作为数组传递

As for the Math.Matrix method, it is from the f# powerpack that you need to install...

you can read this Stackoverflow thread..
what-happened-to-microsoft-fsharp-math-matrix

Also for the Split issue.. Split is a method on a string instance.. You can not call it as a static method as you did..

ex: "Some string value".Split([|' '|]) is a correct approach to split a string passing the list of delimiters as an array

小女人ら 2024-12-13 07:44:47

现在,通过 f# 8,他们引入了 _.Property 作为 fun x -> 的简写x.属性。它将使事情变得更简单,并减少为了那些包装方法和属性的方便的模块功能而打开 PowerPack 或 FSharpX 的需要。

let read_matrix filename =
  lines_of_file filename
  |> Seq.map _.Split(' ')    // <-- new stuff
  |> Seq.map (Seq.map float)
  |> Math.Matrix.ofSeq

更多详细信息请参见此处

Now with f# 8, they introduced _.Property as a shorthand for fun x -> x.Property. It will make things simpler and reduce the need to open PowerPack or FSharpX just for those convenient module functions wrapping methods and properties.

let read_matrix filename =
  lines_of_file filename
  |> Seq.map _.Split(' ')    // <-- new stuff
  |> Seq.map (Seq.map float)
  |> Math.Matrix.ofSeq

More details in this other answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文