9
votes

I am writing a very beginner's F# program (F# Core & Visual Studio Code) as follows:

1.Sort.fs

namespace Demo

module Sort =
    let rec quickSort list =
        match list with
        | [] -> []
        | head::tail ->
            let smalls =
                tail |> List.filter(fun c-> c<head)|> quickSort
            let bigs =
                tail |> List.filter(fun c-> c>=head)|> quickSort
            List.concat [smalls;[head];bigs]        

2.Program.fs

namespace Demo

open Sort

module Program =
    let list = [3;1;8;4;9;5;7;6]

    [<EntryPoint>] 
    let main argv =
        list |> Sort.quickSort |> printfn "%A"       
        printfn "Hello World from F#!"
        0

However when I try to open Sort module into Main I am getting following errors:

The namespace or module 'Sort' is not defined.

The value, namespace, type or module 'Sort' is not defined. Maybe you want one of the following: sqrt

Where as, if I take sort module under same file - `Program.fs, everything works fine. Is there anything else needed to refer file as well?

1
File order is important in F# (for good reason). This will only work if Sort.fs comes before Program.fs in your project. - TheQuickBrownFox
@TheQuickBrownFox, just for my knowledge - may I know where can I find these all or required good reasons? - Avi Kenjale
You can just edit the fsproj file by hand (i.e. move the line with Alt-Up/Down) or use its UI with the Projext Explorer to order the files in VS Code. You can have so called recursive modules and types though where you can refer to a type before its definition. - s952163

1 Answers

7
votes

The order of files in the project explorer is very important. If you want to use module Sort from module Program, Sort.fs has to appear before Program.fs.

more information can be found here, here and here