F# 中的 String.split 问题
我在使用以下代码时遇到问题,我正在尝试构建一个词法分析器。
我再次使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一些问题。你的外壳错了。它是
分割
,不是分割
。这是一个实例(非静态)方法。分隔符必须是数组,而不是列表。以下工作:顺便说一句,
Math.Matrix.of_seq
已被弃用。现在是Math.Matrix.ofSeq。There are a few problems. Your casing is wrong. It's
Split
, notsplit
. It's an instance (not static) method. The separators must be an array, not list. The following works:Incidentally,
Math.Matrix.of_seq
has been deprecated. It is nowMath.Matrix.ofSeq
.为了使实例方法在 F# 中使用起来更自然,常见的做法是定义简单的辅助函数:
A common thing to do to make instance methods more natural to use in F# is to define simple helper functions:
另外对于 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
至于 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
现在,通过 f# 8,他们引入了
_.Property 作为
fun x -> 的简写x.属性
。它将使事情变得更简单,并减少为了那些包装方法和属性的方便的模块功能而打开 PowerPack 或 FSharpX 的需要。更多详细信息请参见此处。
Now with f# 8, they introduced
_.Property
as a shorthand forfun 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.More details in this other answer here.