With C, I can write a program with a generic text editor (i.g. nano) and compile it in the Lunix terminal with
gcc mySum.c -o mySum
to get a window asking for inputs and returning the 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;
}
Can I produce the same kind of program in F#, without Visual Studio/MonoDevelopment?
I found very instructive for me working with a nude text editor, like I am doing with C. It imposes me to be more concentrated on learning, with less help by the IDE. Furthermore, a text editor (such as nano, or notepad++ or whatever) provides more flexible tools than fsharpi
to write the progra canm. In this way, when the program is completed, I give it to the compiler in the same way I did with C in the example.
I would say by implementing the function
let mySum x y =
x + y
with
fsharpc mySum.fs
but I fail to get how to achieve it. I found this reference but it is a little advanced.
The Fortran 77 compilers are usually called f77. The output from the compilation is given the somewhat cryptic name a. out by default, but you can choose another name if you wish. To run the program, simply type the name of the executable file, for example a.
Detailed explanation of the compilation processThe f77 command performs this step by calling the link editor and producing the executable file "main.exe". The final step is to link them together to make the executable file "main.exe".
I think what you want is FSI (F# Interactive), which is called fsharpi
on Linux / MacOS. You can also use fsharpc
, which will compile it, but if you are just trying out, testing or scripting then the REPL environment that fsharpi
gives you is probably more convenient.
An introduction for using it from Linux can be found here.
Note that you must at least have Mono installed before you can do anything with F# in Linux.
EDIT: As illustration, the program you mention in C can be expressed in a myriad of ways, here's one way (which assumes correct input, or exception, program not tested):
[<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" x y (x + y)
// wait before returning prompt
printfn "Hit any key to continue"
Console.ReadKey() |> ignore
EDIT 2: you edited your question and expressed your wish to work with "Nano". That is an editor that I don't know. You also say that you don't want to use Mono. I don't know why that is, unless you mean MonoDevelop, because without Mono you cannot compile a .NET program on Linux.
The command you mention, gcc mySum.c -o mySum
can be translated into a commandline variant for F#. For instance (assuming same syntax as for fsc.exe
) (put on multiple lines for clarity):
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 can possibly be left out, but this is a valid commandline, taken from Visual Studio.
I would, however, suggest to use an editor that is preconfigured to run F# programs, that has a REPL integrated, as well as a debugger, syntax coloring, live type information (very important!) and other intellisense features, which you get with Visual Studio (the VS Code edition runs on Linux and has an excellent editor, courtesy to Guy Coder for reminding me) and possibly some other F# enabled editors out there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With