June 21, 2023

F# -- books

Plenty are available. I just ordered "Programming F# 3.0" -- a used copy for $12.89. This should be the latest edition (2012 - 2nd edition) which is $28 full price.

F# - can we do this on linux?

I have learned now how to use "dotnet new" and "dotnet run" to compile and run C# programs. What about F# I follow the microsoft example verbatim. Note that I am collecting all of my C# and F# experiments in my C# directory.
cd C#
dotnet new sln -o fsample
cd fsample
dotnet new classlib -lang "F#" -o src/Library
After this, the file src/Library/Library.fs contains:
namespace Library

module Say =
    let hello name =
        printfn "Hello %s" name
As instructed, I change this to:
module Library

open System.Text.Json

let getJson value =
    let json = JsonSerializer.Serialize(value)
    value, json
After this, back at the top level, I do:
pwd
/home/tom/C#/fsample
dotnet sln add src/Library/Library.fsproj
Project `src/Library/Library.fsproj` added to the solution.

dotnet build
MSBuild version 17.4.3+7e646be43 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  Library -> /u1/home/tom/C#/fsample/src/Library/bin/Debug/net7.0/Library.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.14
Continuing with their instructions:
dotnet new console -lang "F#" -o src/App
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /u1/home/tom/C#/fsample/src/App/App.fsproj:
  Determining projects to restore...
  Restored /u1/home/tom/C#/fsample/src/App/App.fsproj (in 255 ms).
Restore succeeded.
After this the file fsample/src/App/Program.fs contains:
// For more information see https://aka.ms/fsharp-console-apps
printfn "Hello from F#"
As instructed, I change this to:
open System
open Library

[]
let main args =
    printfn "Nice command-line arguments! Here's what System.Text.Json has to say about them:"

    let value, json = getJson {| args=args; year=System.DateTime.Now.Year |}
    printfn $"Input: %0A{value}"
    printfn $"Output: %s{json}"

    0 // return an integer exit code
Then at the top level:
[tom@trona fsample]$ pwd
/home/tom/C#/fsample
[tom@trona fsample]$ dotnet add src/App/App.fsproj reference src/Library/Library.fsproj
Reference `..\Library\Library.fsproj` added to the project.
[tom@trona fsample]$ dotnet sln add src/App/App.fsproj
Project `src/App/App.fsproj` added to the solution.
[tom@trona fsample]$ dotnet restore
  Determining projects to restore...
  Restored /u1/home/tom/C#/fsample/src/App/App.fsproj (in 198 ms).
  1 of 2 projects are up-to-date for restore.
[tom@trona fsample]$ dotnet build
MSBuild version 17.4.3+7e646be43 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  Library -> /u1/home/tom/C#/fsample/src/Library/bin/Debug/net7.0/Library.dll
  App -> /u1/home/tom/C#/fsample/src/App/bin/Debug/net7.0/App.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.85
And now we are ready to run it:
[tom@trona fsample]$ cd src/App
[tom@trona App]$ dotnet run Hello World
Nice command-line arguments! Here's what System.Text.Json has to say about them:
Input: { args = [|"Hello"; "World"|] year = 2023 }
Output: {"args":["Hello","World"],"year":2023}
All of this works exactly as they say. I did not need to install anything extra to start doing all of this with F#. But it is all very mysterious.

It is important to note that the first link (at the very top of this page) offers a choice. Linux people can use Visual Studio Code or they can work from the command line. All of the above is the "command line" way. Perhaps using VS code (supposedly just an editor?) would lead to a less confusing methodology.

This requires something called "Ionide" and you get it like so:
cd /home/tom/C#
dotnet new console -lang "F#" -o FirstIonideProject
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /u1/home/tom/C#/FirstIonideProject/FirstIonideProject.fsproj:
  Determining projects to restore...
  Restored /u1/home/tom/C#/FirstIonideProject/FirstIonideProject.fsproj (in 268 ms).
Restore succeeded.
Then you dive into that new project and launch code:
cd FirstIonideProject
code .
Note the "dot" after code. I do this, but as I reread the microsoft instructions, they tell me I need the Ionide plugin also. So I go to extensions and search for Ionide and find "Ionide for F#" and click on Install. They also mention that I should "configure Visual Studio Code to use .NET Core scripting". This is not explained, but this link may help: Based on this, I install the .NET extension pack (which includes F# Ionide and C# Dev Kit). I also install the .NET Core extension pack. I exit and restart code and resume the "Getting Started" tutorial. I create MyFirstScript.fsx as follows:
let toPigLatin (word: string) =
    let isVowel (c: char) =
        match c with
        | 'a' | 'e' | 'i' |'o' |'u'
        | 'A' | 'E' | 'I' | 'O' | 'U' -> true
        |_ -> false

    if isVowel word[0] then
        word + "yay"
    else
        word[1..] + string(word[0]) + "ay"
And I follow their instruction (with success!) to run this function using FSI.

Learning F#

The next step (as they say) is this:


Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org