Compile an F # program on Linux using the command line

With C, I can write a program with a universal text editor (e.g. nano) and compile it on a Lunix terminal using

gcc mySum.c -o mySum 

to get a window asking you to input and return output.

 #include <stdio.h> int main(){ int x; int y; int sum; printf("Program that sums two numbers.\n\nPlease type the first one:\n"); scanf("%d", &x); printf("Type the second one:\n"); scanf("%d", &y); sum = x + y; printf("The sum of %d and %d is %d\n", x, y, sum); return 0; } 

Is it possible to create the same program in F # without Visual Studio / MonoDevelopment?

I was very instructive to work with a nude text editor, for example, I do it with C. This puts me very interested in learning, with less help from the IDE. In addition, a text editor (such as nano or notepad ++ or something else) provides more flexible tools than fsharpi for writing programs. Thus, when the program is completed, I pass it to the compiler in the same way as in Example C.

I would say implementing a function

 let mySum xy = x + y 

from

 fsharpc mySum.fs 

but I can’t figure out how to achieve it. I found this link , but made some progress.

+6
source share
1 answer

I think you want FSI (F # Interactive) called fsharpi on Linux / MacOS. You can also use fsharpc , which will compile it, but if you are just trying, testing or creating scripts, then you can find it here .

Note that you must at least install Mono before you can do anything with F # on Linux.

EDIT: As an illustration, the program you mentioned in C can be expressed in a variety of ways, here is one way (which assumes the correct input or exception, the program has not been verified):

 [<EntryPoint>] let main argv = let scani() = printfn "Give a number: " Console.ReadLine() |> Int64.Parse let (x, y) = (scani(), scani()) printfn "The sum of %d and %d is %d" xy (x + y) // wait before returning prompt printfn "Hit any key to continue" Console.ReadKey() |> ignore 

EDIT 2: You edited your question and expressed a desire to work with Nano. This is an editor I do not know. You also say that you do not want to use Mono. I don’t know why this is so if you do not mean MonoDevelop, because without Mono you cannot compile a .NET program on Linux.

The gcc mySum.c -o mySum command you gcc mySum.c -o mySum can be converted to the command line option for F #. For example (given the same syntax as for fsc.exe ) (for greater clarity on several lines):

 fsharpc.exe -o:MyProgram.exe --debug:pdbonly --noframework --optimize+ --platform:anycpu32bitpreferred -r:"PathTo\FSharp.Core.dll" -r:"PathTo\mscorlib.dll" -r:"PathToYourReferencedOtherDlls\SomeClassLib.dll -r:"PathTo\System.Core.dll" -r:"PathTo\System.dll" --target:exe --warn:3 file1.fs file2.fs file3.fs 

Many of these options may be omitted, but this is a valid command taken from Visual Studio.

I would suggest using an editor that is preconfigured to run F # programs, which has an integrated REPL, as well as a debugger, syntax coloring, live type information (very important!), And other intellisense functions that you get with Visual Studio ( VS version Code runs on Linux and has a great editor, kindly provided by Guy Coder to remind me) and maybe some other F # editors are included.

+4
source

Source: https://habr.com/ru/post/1013299/


All Articles