Examples
A few small samples follow:
// This is a comment for a sample hello world program. printfn "Hello World!"A Person class with a constructor taking a name and age and two properties.
/// This is a documentation comment for a type definition. type Person(name : string, age : int) = member x.Name = name member x.Age = ageA 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#:
/// This is a documentation comment for a function. let rec factorial n = match n with | 0 -> 1 | _ -> n * factorial (n - 1)Iteration examples:
/// Iteration using a 'for' loop let printList lst = for x in lst do printfn "%d" 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 printList tFibonacci examples:
/// Fibonacci Number formula let rec fib n = match n with | 0 | 1 -> n | _ -> fib (n - 1) + fib (n - 2) /// Another approach - a lazy infinite sequence of Fibonacci numbers let fibSeq = Seq.unfold (fun (a,b) -> Some(a+b, (b, a+b))) (1,1) // Print even fibs |> 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 ] |> printListA sample Windows Forms program:
// 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 = sprintf "x = %d" x) // Add the label to the form form.Controls.Add(label) // Finally, run the form Application.Run(form)Asynchronous parallel programming sample (parallel CPU and I/O tasks):
/// A very naive 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")Read more about this topic: F Sharp (programming Language)
Famous quotes containing the word examples:
“Histories are more full of examples of the fidelity of dogs than of friends.”
—Alexander Pope (16881744)
“It is hardly to be believed how spiritual reflections when mixed with a little physics can hold peoples attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.”
—G.C. (Georg Christoph)
“In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.”
—Michel de Montaigne (15331592)