F Sharp (programming language)

From Wikipedia, the free encyclopedia

Jump to: navigation, search
F#
Paradigm multi-paradigm: imperative, functional, object-oriented
Appeared in 2005 (last revised 2008)
Designed by Microsoft Research
Developer Microsoft Research
Latest release 1.9.6.2/ August 29, 2008
Typing discipline static, strong, inferred
Influenced by Objective Caml, C#, Haskell[citation needed]
OS Cross-platform (.NET Framework, Mono)
License Microsoft Research Shared Source license agreement ("MSR-SSLA")
Website F# at Microsoft Research

F# (pronounced F Sharp) is a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative object-oriented programming disciplines. It is a variant of ML and is largely compatible with the OCaml implementation. F# was initially developed by Don Syme at Microsoft Research but is now being developed at Microsoft Developer Division and will be distributed as a fully supported language in the .NET Framework and Visual Studio ecosystem as part of Visual Studio 2010.[1]

Contents

[edit] Overview

F# is a strongly-typed language that uses type inference. As a result, data types need not be explicitly declared by the programmer; they will be deduced by the compiler during compilation. However, F# also allows explicit data type declaration. Being a .NET language, F# supports .NET types and objects. But it extends the type system and categorizes types as immutable types or mutable types. .NET objects classify as mutable types (which can be edited in-place), and are used to provide an object-oriented programming model. Immutable types (editing which creates a new instance without overwriting the older one) are primarily used for functional programming.

Like ML, F# includes a functional programming component supporting eager evaluation. For functional programming, it provides several constructs and a set of immutable types: tuples, records, discriminated unions and lists.[2] A tuple represents a collection of more than one value. It is represented as (A, B), where A and B can be of any type. A tuple can be used only to store values when the number of values is known at design-time and stays constant throughout execution. A record is a specialization of tuple where the data members are named, as in { Name:string; Age:int }. Records can be created as { Name="AB"; Age=32 } The with keyword is used to create a copy of a record, as in { r with Name="CD" } creates a new record that by changing the value of the Name field in record r (assuming the record created in the last example was named r). The list type is a regular linked list represented either using a head::tail notation (composition using the ::-cons operator) or a shorthand as [item1; item2; item3]. An empty list is defined as [].

An algebraic data type (which is functionally like a type-safe version of C union) can be defined to hold a value of any of a pre-defined type. For example,

type A = 
   | ConstructorA of string
   | ConstructorB of int

can hold values as instantiated by either constructor. The type of the values the constructors will act on can be defined as well. Constructors are used to create a view of the data type different from the actual implementation, as required for supporting the Active Patterns concept.[2] Data types are created with the type keyword. F# uses the let keyword for binding type values to a name (variable).[2]

F# uses pattern matching to resolve names into values. It is also used when accessing discriminated unions. Functions using a discriminated union provide different expressions to be invoked, associated with the data type in the union. The union is matched against these data types, and the expression associated with the match is invoked. F# also supports the Active Patterns pattern. It is used, for example, when a type needs to provide multiple views. For example, an exponential number will provide both the final value, as well as the base and exponents.[2]

All functions in F# are instances of the function type, and are immutable as well.[2] Functions can either be curried or in uncurried form. Being an instance of a type, functions can be passed as arguments to other functions, resulting in higher order functions. F# supports lambda functions and closures as well.[2] Like other functional programming languages, F# allows function composition using the >> operator. Every statement in F#, including if statements and loops, are composable expressions with a definite return type as well.[2] Functions and expressions that do not return any value have a return type of unit.

The F# extended type system is implemented as generic .NET types. The Record type creates a .NET class with the field names as properties. Tuples are generic classes of type Tuple<_,_>. the number of type parameters define the number and types of the elements in the tuple. Discriminated unions are implemented as tagged unions. Functions are of type FastFunc<_,_> with type parameters specifying the parameter and return types.[3]

F#, like other .NET languages, can use .NET types and objects, using an imperative object-oriented style of programming. For imperative programming, F# supports for and while loops, arrays (created with the [| ... |] syntax, and number sequences written in shorthand as in 1 .. 25) and support for creating Object types (equivalent to .NET classes).[2] F# also allows extending the syntax to support embedding custom domain-specific languages within the F# language itself.[2]

F# provides sequence expressions[4] that allows for a defining a sequence block (seq { ... } or [ ... ] or [| ... |]) encapsulating constructs (either functions, conditional expressions or loops) that act on a collection such that the results and another function (or lambda), such that the function is invoked on the results yielded from the collection collection-processing expressions. For example, seq { for b in 0 .. 25 when b < 15 -> (b*b) } is a sequence expression that forms a sequence of squares of numbers from 0 to 14 by filtering out numbers from the range of numbers from 0 to 25. The sequence is lazily evaluated, i. e., the collection is processed and results yielded on-demand. It can be used for filtering and is the basis of support for LINQ queries. Sequence expressions are generalized as Computation Expressions which are equivalent to monads.[4]

Sequence expressions and computation expressions are also used for creating asynchronous workflows.[5] An asynchronous workflow is defined as a sequence of commands inside a async{ ... }, as in

let asynctask = async
{
    let req = WebRequest.Create(url)
    let! response = req.GetResponseAsync()
    use stream = response.GetResponseStream()
    use streamreader = new System.IO.StreamReader(stream)
    return streamreader.ReadToEnd()
}

The let! allows the rest of the async block to be defined as the delegate and passed as the callback function of an asynchronous operation. This helps deal with inversion of control issues.[5] The async block is invoked using the Async.Run function. Multiple async blocks are executed in parallel using the Async.Parallel function that takes a list of async objects (in the example, asynctask is an async object) and creates another async object to run the tasks in the lists in parallel. The resultant object is invoked using Async.Run.[5]

F# comes with a Microsoft Visual Studio language service that integrates it with the IDE. With the language service installed, Visual Studio can be used to create F# projects and the Visual Studio debugger used to debug F# code. In addition, it comes with a Visual Studio-hosted interactive console that executes F# code as it is being written.

[edit] Examples

A few small samples follow:

(* This is commented *)
(* Sample hello world program *)
printfn "Hello World!"

A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative integers, here shown in F#:

#light
open Microsoft.FSharp.Math
let rec factorial (n :BigInt) =
    match n with
    | 0I -> 1I
    | _ -> n * factorial (n - 1I)
#light
open Microsoft.FSharp.Collections.List
(* print a list of numbers recursively *)
let rec printlist lst =
    if lst = []
    then ()
    else printf "%d\n" (nth lst 0)
         printlist (tl lst)
 
(* Same thing, using matching against list elements *)
let rec printlist2 l =
    match l with
    | []     -> ()
    | h :: t -> printfn "%A" h
                printlist2 t
 
(* Using shorthand for match *)
let rec printlist3 = function
    | []     -> ()
    | h :: t -> printfn "%A" h
                printlist3 t
 
(* Or, using a higher-order function *)
let printlist4 lst = List.iter (printfn "%A") lst
#light
(* Fibonacci Number formula *)
let rec fib n =
    match n with
    | 0 | 1 -> n
    | _ -> fib (n - 1) + fib (n - 2)
 
(* An alternative approach - a lazy recursive sequence of Fibonacci numbers *)
let rec fibs = seq {
    yield! [1; 1];
    for (x, y) in Seq.zip fibs (Seq.skip 1 fibs) -> x + y }
 
(* Print even fibs *)
[1 .. 10]
|> List.map     fib
|> List.filter  (fun n -> (n mod 2) = 0)
|> printlist
 
(* Same thing, using Comprehension syntax *)
[ for i in 1..10 do
    let r = fib i
    if r % 2 = 0 then yield r ]
|> printlist
#light
(* Sample Windows Forms Program *)
 
(* We need to 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 temp = new Label()
    let x = 3 + (4 * 5)
    (* Set the value of the Text*)
    temp.Text <- sprintf "x = %d" x
    (* Remember to return a value! *)
    temp
 
(* Add the label to the form *)
do form.Controls.Add(label)
 
(* Finally, run the form *)
[<STAThread>]
do Application.Run(form)
#light
(* async workflows sample (parallel tasks) *)
 
(* very naive prime number detector *)
let is_prime n =
    {2 .. n-1} |> Seq.exists (fun x -> n % x = 0) |> not
 
(* we are using async workflows *)
let primeAsync n =
    async { return (n, is_prime n) }
 
(* return primes between m and n using multiple threads *)  
let primes m n =
    {m .. n}
        |> Seq.map primeAsync
        |> Async.Parallel
        |> Async.Run
        |> Array.filter snd
        |> Array.map fst
 
(* run a test *)
primes 1000000 1002000
    |> Array.iter (printfn "%d")

[edit] See also

[edit] References

[edit] External links

Personal tools