Search code examples
stringf#lexer

Trouble with String.split in F#


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.


Solution

  • 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.