F#
Online F# Compiler
main.fs is compiled with the F# 10 compiler and runs on .NET 10. Console.ReadLine() waits for you to type in the terminal, and printf prompts show before it does.
Try it aboveHow F# runs here
- Versions
- F# 10 on .NET 10
- Build
fsc main.fs- Run
dotnet main.dll- Limits per run
- 512 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
An F# program that reads input
This main.fs reads numbers in a list expression and pipes them through List functions:
open System printf "How many numbers? " let n = Console.ReadLine() |> int let nums = [ for i in 1 .. n do printf "Number %d: " i yield Console.ReadLine() |> int ] let sorted = List.sort nums printfn "Sorted: %s" (sorted |> List.map string |> String.concat " ") printfn "Sum: %d, average: %.2f" (List.sum nums) (List.averageBy float nums)
$ fsc main.fs && dotnet main.dll How many numbers? 4 Number 1: 12 Number 2: 7 Number 3: 30 Number 4: 5 Sorted: 5 7 12 30 Sum: 54, average: 13.50
Reading input in F#
- Console.ReadLine() (or stdin.ReadLine()) returns the line as a string; convert it with int or float.
- For several numbers on one line: Console.ReadLine().Split(' ') |> Array.map int.
- int on text that isn’t a number throws System.FormatException. Int32.TryParse returns a bool and the value, so you can ask again.
What isn’t available here
- NuGet packages: #r "nuget: ..." works only in F# Interactive scripts, and this compiles main.fs with fsc. The .NET 10 base library and FSharp.Core are there.
- More than one file: only main.fs is compiled.
- Culture-specific formatting: .NET runs in globalization-invariant mode here, so numbers print with a dot as the decimal separator.
Errors you’ll see
- “The value or constructor 'prinfn' is not defined.” (error FS0039) A typo, or a name defined further down the file: F# reads top to bottom.
- A type mismatch (error FS0001), for example a string where an int was expected: convert input with int first.
- Possible incorrect indentation: F# uses indentation for blocks, so a line that doesn’t line up with its block changes the meaning or fails to compile.
Questions
Which F# version does this use?
F# 10 on .NET 10, compiled with fsc and run with dotnet.
Can I use NuGet packages?
No. Only the .NET base library and FSharp.Core are referenced, and runs have no network.
How do I read input in F#?
let n = Console.ReadLine() |> int, after open System. Type the input in the terminal while the program runs.