Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Refer a module function from another file

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?

like image 554
Avi Kenjale Avatar asked Dec 14 '17 14:12

Avi Kenjale


1 Answers

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

like image 107
Irakli Gabisonia Avatar answered Oct 09 '22 06:10

Irakli Gabisonia