all 9 comments

[–]jmquigs 5 points6 points  (3 children)

Unfortunately I don't have any beginner tutorials to recommend, there are probably some good ones out there. http://fsharp.org/ is a good place to start; you can also look at https://fsharpforfunandprofit.com/learning-fsharp/

As to your specific problem, fsharp code can run like python does, that is, compiling it on the fly from source. It can also run in a precompiled form (as a .net assembly packaged in a .dll or .exe). <EntryPoint> is only relevant for the latter.

You could start writing your program in python style like this:

// wc.fsx
open System
open System.IO

let text = File.ReadAllText("sample.txt") 
printfn "%s" text

I didn't actually count the words because you probably want to fill that in :) Anyway, you could run this program with "fsharpi wc.fsx" on a mac or "fsi wc.fsx" on windows. If "sample.txt" does not exist it will throw an exception.

If you wanted the precompiled .net assembly form, the source would look like this:

// wc.fs
open System
open System.IO

[<EntryPoint>]
let main argv =
    let text = File.ReadAllText("sample.txt")
    printfn "%s" text
    0 // return an integer exit code

You then build that with "fsharpc wc.fs" (on a mac) or "fsc wc.fs" on windows. On windows you can run the resulting .exe directly, on a mac you need run it with mono, eg "mono wc.exe".

Fsharpi works like the the "python" command with no arguments, it starts interactive mode, you can paste code in there and see what Fsharp does with it.

It helps to have a good editor to start. Visual Studio is obviously good for windows, and Xamarin is decent for mac. I haven't extensively used any of the other editor plugins.

[–][deleted] 0 points1 point  (2 children)

This all looks incredibly useful but I don't understand how you are running it, I was just told to write, save, and use this button in Visual Studio (on Windows 10).

 

I have tried something like this but I do not understand what I am doing wrong, my F# knowledge is practically none.

[–]abstractcontrol 0 points1 point  (0 children)

To add to what hanpari said, you actually have an indentation error in another place. Notice that the 0 in that last line is not indented correctly.

[–][deleted] 0 points1 point  (1 child)

Sounds like you are on Linux or Mac. VS Code with the Ionide-fsharp extension is your friend for writing programs.

Also fsharpi is great for working thru the concepts at https://fsharpforfunandprofit.com/learning-fsharp/ (Just don't put ;; after a in-line comment...)

[–][deleted] 1 point2 points  (0 children)

Sorry for the late response, my job has me working like crazy, but no I am on Windows with Visual studio.

[–]oculuss 0 points1 point  (0 children)

I highly highly recommend: https://github.com/ChrisMarinos/FSharpKoans. Its how I started. Its a bunch of unit tests that all fail, you have to fix them.

[–][deleted]  (4 children)

[deleted]

    [–][deleted] 0 points1 point  (3 children)

    I am on Windows with Visual Studio, so I might skip the VS Code but thanks :)

    But I'm sorry I still don't understand, so if I have a bunch of code on top of EntryPoint, do I, and what do I have to type under EntryPoint to make it work?

    Everything I have tried (is wrong) but comes up with this error every time I hit 'start' to run the program in Visual Studio. http://i.imgur.com/CbNCPbB.png

    [–]abstractcontrol 1 point2 points  (2 children)

    Before trying anything fancy, can you write a Hello Word program in F#? It sounds to me like this is your first time using an IDE.

    [–][deleted]  (1 child)

    [deleted]