Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
F Sharp (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== A few small samples follow: <syntaxhighlight lang="fsharp"> // This is a comment for a sample hello world program. printfn "Hello World!" </syntaxhighlight> A record type definition. Records are immutable by default and are compared by structural equality. <syntaxhighlight lang="fsharp"> type Person = { FirstName: string LastName: string Age: int } // Creating an instance of the record let person = { FirstName = "John"; LastName = "Doe"; Age = 30 } </syntaxhighlight> A Person class with a constructor taking a name and age and two immutable properties. <syntaxhighlight lang="fsharp"> /// This is a documentation comment for a type definition. type Person(name : string, age : int) = member x.Name = name member x.Age = age /// class instantiation let mrSmith = Person("Smith", 42) </syntaxhighlight> A simple example that is often used to demonstrate the syntax of functional languages is the [[factorial function]] for non-negative 32-bit integers, here shown in F#: <syntaxhighlight lang="fsharp"> /// Using pattern matching expression let rec factorial n = match n with | 0 -> 1 | _ -> n * factorial (n - 1) /// For a single-argument functions there is syntactic sugar (pattern matching function): let rec factorial = function | 0 -> 1 | n -> n * factorial (n - 1) /// Using fold and range operator let factorial n = [1..n] |> Seq.fold (*) 1 </syntaxhighlight> Iteration examples: <syntaxhighlight lang="fsharp"> /// Iteration using a 'for' loop let printList lst = for x in lst do printfn $"{x}" /// Iteration using a higher-order function let printList2 lst = List.iter (printfn "%d") lst /// Iteration using a recursive function and pattern matching let rec printList3 lst = match lst with | [] -> () | h :: t -> printfn "%d" h printList3 t </syntaxhighlight> Fibonacci examples: <syntaxhighlight lang="fsharp"> /// Fibonacci Number formula [<TailCall>] let fib n = let rec g n f0 f1 = match n with | 0 -> f0 | 1 -> f1 | _ -> g (n - 1) f1 (f0 + f1) g n 0 1 /// Another approach - a lazy infinite sequence of Fibonacci numbers let fibSeq = Seq.unfold (fun (a,b) -> Some(a+b, (b, a+b))) (0,1) // Print even fibs [1 .. 10] |> List.map fib |> List.filter (fun n -> (n % 2) = 0) |> printList // Same thing, using a list expression [ for i in 1..10 do let r = fib i if r % 2 = 0 then yield r ] |> printList </syntaxhighlight> A sample Windows Forms program: <syntaxhighlight lang="fsharp"> // Open the Windows Forms library open System.Windows.Forms // Create a window and set a few properties let form = new Form(Visible=true, TopMost=true, Text="Welcome to F#") // Create a label to show some text in the form let label = let x = 3 + (4 * 5) new Label(Text = $"{x}") // Add the label to the form form.Controls.Add(label) // Finally, run the form [<System.STAThread>] Application.Run(form) </syntaxhighlight> Asynchronous parallel programming sample (parallel CPU and I/O tasks): <syntaxhighlight lang="fsharp"> /// A simple prime number detector let isPrime (n:int) = let bound = int (sqrt (float n)) seq {2 .. bound} |> Seq.forall (fun x -> n % x <> 0) // We are using async workflows let primeAsync n = async { return (n, isPrime n) } /// Return primes between m and n using multiple threads let primes m n = seq {m .. n} |> Seq.map primeAsync |> Async.Parallel |> Async.RunSynchronously |> Array.filter snd |> Array.map fst // Run a test primes 1000000 1002000 |> Array.iter (printfn "%d") </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)