open System
//functions
let multiply (x,y) = x*y
Console.WriteLine(multiply (5,2))
//range of numbers
let k = [for x in 1..10-> x*x]
Console.WriteLine(k)
//point function
let m (a,b,c) = a+b*c
Console.WriteLine(m(2,3,4))
//recursive function
let rec fact x = if x <= 1 then 1 else x*fact(x-1)
Console.WriteLine (fact(5))
//mutually recursive function
let rec isOdd x =
if x = 0 then false
elif x = 1 then true
else isEven(x-1)
and isEven x =
if x = 0 then true
elif x = 1 then false
else isOdd (x-1)
Console.WriteLine("_________")
Console.WriteLine( isOdd(5))
//swap function
let swap (a, b) = (b, a)
Console.WriteLine (swap(4,5))
//Square function
let dnumb = [for x in 1..5 -> x*2]
Console.WriteLine (dnumb)
//Values as functions
let square = fun x -> x*x
Console.WriteLine (square(3))
//pattern matching using a function *doesnt use match keyword
let rec factorial = function
| n when n < 1 -> 1
| n -> n * fact (n - 1)
Console.WriteLine (fact(5))
let rec fibbonaci = function
| x when x < 1 -> 1
| x -> fibbonaci (x-1) + fibbonaci (x-2)
Console.WriteLine(fibbonaci(7))
//pattern matching without a function *use match and with keywords
let isOdd x = (x%2 = 1)
let describeNo x = match isOdd x with
| true ->printfn "x is odd"
| false ->printfn"x is even"
describeNo 6
let rec fib n =
match n with
| 0 -> 0
| 1 -> 1
| _ -> fib (n - 1) + fib (n - 2)
for i = 1 to 10 do
printfn "Fibonacci %d: %d" i (fib i)
let rec fac x = match x with
| x when x < 1 -> 1
| x -> x*fac(x-1)
Console.WriteLine(fac(5))
//Lists
//Creating lists
let list1 = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
//[] the last element should be empty
let list2 = 1::2::3::4::5::6::7::8::9::10::[]
Console.WriteLine (list1)
Console.WriteLine(list2)
//The number of elements
Console.WriteLine(List.length list1)
//The sum of the elements in the list
Console.WriteLine(List.sum list1)
//prints out the head = first element
Console.WriteLine(List.head list1)
//prints out the tail = the rest of the elements
Console.WriteLine(List.tail [1;2;3;4])
//Create list with range
let list3 = [1..2..10]
Console.WriteLine(list3)
//Yield
let numbersNear x = [
yield x-1
yield x
yield x+1
]
Console.WriteLine(numbersNear 4)
//do yield can be used in 2 ways
//1)using do yield
let list4 = [ for a in 1 .. 10 do yield (a * a) ]
Console.WriteLine(list4)
//2) using ->
let list5 = [for x in 1 .. 10 -> x*2 ]
Console.WriteLine(list5)
let list = [for a in 1 .. 5 do
match a with
| 3 -> yield! ["p"; "c"; "l"]
| _ -> yield a.ToString ()
]
Console.WriteLine(list)
About Online Fsharp Compiler
Try our Online Fsharp Compiler (Version F# 4.1) to Edit, Run, and Share your Fsharp Code directly from your browser. This online development environment provides you the latest version F# 4.1.
How to use Online Fsharp Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Fsharp Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Fsharp Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Fsharp Compiler: Save and Share Fsharp Code (Project)
Save Fsharp Project Online
You can save your Fsharp Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Fsharp Project Online
You can use this feature to share your Fsharp Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Fsharp Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Fsharp Compiler
There are several benefits of using the Online Fsharp Compiler to run your Fsharp code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.