Generating F# Sequences From Console Input

I got a very nice answer on SO to why a function was not executing.


open System
open System.Collections.Generic
open System.Text
open System.IO
#nowarn "40"

let rec readlines () =
     seq {
           let line = Console.ReadLine()
           if not (line.Equals("")) then
              yield line
              yield! readlines ()
}

[<EntryPoint>]
let main argv =
      let inSeq = readlines ()

      inSeq
      |> Seq.length
      |> printfn "%d lines read"

      // This will keep it alive enough to read your output
      Console.ReadKey() |> ignore
      0

Nice F# List.fold example

Got this example from here.


open System
open System.Threading
open System.Collections.Generic
open System.Linq
open System.Text
open System.Threading.Tasks
open System.IO
open Microsoft.VisualBasic.FileIO


let main argv =
   let data = [(Cats,4);
               (Dogs,5);
               (Mice,3);
               (Elephants,2)]

   let count = List.fold (fun acc (nm,x) acc+x) 0 data
   printfn "Total number of animals: %d" count


Even though I’ve used map and other functions in Clojure, I’ve forgotten the basics of an inline function. The value of 0 is assigned to nm, and x takes on the first row of data.

 

Reading in a .csv file

Yes, it is my favorite subject, which is transforming data in .csv files. Here’s reading data into a variable in F#.


open System
open System.Threading
open System.Collections.Generic
open System.Linq
open System.Text
open System.Threading.Tasks
open System.IO
open Microsoft.VisualBasic.FileIO

[<EntryPoint>]
let main argv =
  let csv_fileH = new TextFieldParser(test1.csv)
  csv_fileH.TextFieldType = FieldType.Delimited |> ignore
  let x = csv_fileH.SetDelimiters(',')
  let csv_data = new List string[]()

  let eod = csv_fileH.EndOfData

  if not eod then
    let column_headings = csv_fileH.ReadFields()
    csv_data.Add(column_headings) |> ignore

    // Parentheses are needed after function definition to produce type bool.
    let read_rest_of_csv() =
    csv_data.Add(csv_fileH.ReadFields()) |>
    not csv_fileH.EndOfData

    while read_rest_of_csv() do ignore None

  0 // return an integer exit code

This program is associated on stackoverflow with some very good answers to a question I asked.

A First [and Simple] F# Sample

Anything worth learning should not be simple. Learning Clojure was not simple, and hacking with C# for the first time was not simple either, so I should not expect simple while learning F#.

Here is a first small program, because I have found a dirth of regular simple examples out on the web, especially those calling .Net functions.


 open System
 open System.Threading
 open System.Collections.Generic
 open System.Linq
 open System.Text
 open System.Threading.Tasks
 open System.IO
 open Microsoft.VisualBasic.FileIO

[EntryPoint]
let main argv =
    let parser = new TextFieldParser(&quot;test1.csv&quot;)
    parser.TextFieldType = FieldType.Delimited |&amp;amp;amp;gt; ignore
    let x = parser.SetDelimiters(&quot;,&quot;)

    let eod = parser.EndOfData
    if not eod then
        let column_headings = parser.ReadFields()
        printf &quot;%A&quot; column_headings |&amp;amp;amp;gt; ignore
0 // return an integer exit code

This is the contents of test1.csv

&lt;br data-mce-bogus=&quot;1&quot;&gt;

AGY/DIV,STS,GIC-ID,LAST-NAME,FIRST-NAME,COVERAGE DESCRIPTION,PREMIUM,RUN-DATE,BILL MONTH&lt;br data-mce-bogus=&quot;1&quot;&gt;