F Sharp (programming language)
F # is a functional programming language developed by Microsoft for the. NET Framework. F # is related to the languages OCaml and ML, the syntax of the language, therefore, are very similar. Many OCaml programs that avoid unusual constructs and Caml - specific libraries, can therefore be compiled without change with F #.
In addition to the ingredients of functional programming are included in F # object-oriented and imperative language constructs.
Originally F # was developed as a research project by Microsoft Research, presently there will be continued by Microsoft's development department. F # is as part of Visual Studio 2010 for the first time officially supported and delivered. In addition, it is also available as an extension to Visual Studio 2008. In November 2010, F #, and associated compiler and libraries under the Apache License 2.0 was released.
Some language features
Variables
In F # there are no variables as in other programming languages. However, there are functions that always have the same return value. These are in F # declared with the let keyword and defined.
Let pi = 3.1415927 F # has type inference, that is, types of expressions are determined automatically. For example, pi automatically gets the type of floating-point literal.
Let is no variable assignment, but a declaration, ie the source
Let pi = 3.1415927 let pi = 10.0 can not be compiled because the value of pi is declared twice in the same scope. If the value of a variable in the program sequence to be changed, this must also be indicated in the declaration:
Let mutable x = 0 / / x is characterized by the " mutable " keyword as a variable x < - 5 / / x, a new value is assigned printfn "% i" x / / returns 5 from To change values in retrospect is also possible through the use of so-called reference cells:
Let x = ref 0 / / x is of type " int ref" is therefore a reference to an integer x: = 5 / / x is a new value is assigned to printfn "% i"! x / / Using the "! " operator x is dereferenced. Returns 5. functions
Functions are declared like other values using let:
Let square x = x * x
Let add xy = x y Functions, functions as parameters expected ( see higher-order function ):
Let do_twice f x = f ( f x) The statement
Printfn "% A" ( do_twice square 5) are 625 (the square of the square of 5 ) from. The do_twice function and using the composition operator and after eta - reduction as
Do_twice let f = f >> f be written.
In the example above for the square type int - determined > int, that is, square is a function that expects a parameter of type int and returns a value of type int. For do_twice gives the type ( 'a - >' a) -> 'a - >' a This means do_twice is a function as its first parameter a value of type ( 'a - >' a) gets (a function with a parameter of type ' a and a return value of type ' a). As second parameter it is given a value of type 'a and returns a value of type ' a back. 'a has the role of a type variable ( roughly comparable to generic or template parameters in Java / C , see Polymorphism (programming) ) here.
In F #, parameters without parentheses, etc., separated by spaces, to pass to the function. Only when a different function is required as a parameter of the return value, staples must be placed to define the evaluation of the expressions. In printfn " % A" (add 5 8) are the values 5 and 8 to the add function; its return value is a parameter for the function printfn.
F # allows Closures and Currying used:
Let add xy = x y let inc = add 1 In the definition of inc the first parameter of the function is bound to add the value 1. The result of this partial function application is a new function with only one parameter. The evaluation of the expression
Inc 5 supplies as a result 6
F # supports tuple:
Let u = (3, 6) Let V = (2, -3) Let add ( a, b) (C, D ) = (A C, B D) let x, y = add u v F # provides discriminated unions and pattern matching:
/ / An element of type tree is either a " road " and contains two elements of type "tree", / / Or it is a "leaf" and contains an integer type tree = | Branch of tree * tree | Leaf of int let rec x = total tree match x with | Branch (l, r) - > tree sum l r sum tree | Leaf (x ) -> x In F # and object- oriented programming is possible. Example of a class declaration:
Type Person = val name: string val mutable age: int new ( n, a) = { name = n; age = a} member x.Name = x.name member x.Age with get () = x.age and set ( v) = x.age <- v member x.Print () = printfn "% s is% i years old. " x.name x.age F # is compliant with the Common Language Infrastructure. Thus it can be accessed in F # to types that are written in other. NET languages , and vice versa. Null pointers are only required for the interaction with classes from the. NET Framework.
Syntax
In F # code two syntax forms are possible: simple syntax and detailed syntax. The simple syntax is used by default, but with this form, the indents are very important. They play at the detailed syntax of a smaller role, because there determine more keywords like begin, end and in the beginning and end of code blocks.
Example of the simple and detailed syntax:
Let mutable x = 1 while x <3 do x <- x 1 / / let mutable x = 1 while x <3 do x <- x 1 done type Person = val name: string val mutable age: int / / / / type Person = class val name: string val mutable age: int end In the simple syntax, the indenting is mandatory, at the detailed you could omit it as well.
Development environment and compiler
F # code is compiled, this creates intermediate code in the Common Intermediate Language (CIL ), just as with programs written in C # or VB.NET.
F # is not in the free Visual Studio Express Editions included, but one can be installed by a free toolkit. F # is included editions directly from the paid Visual Studio.
There is also an interactive environment or F # interpreter, F # Interactive or shortly FSI. Now we can write the code directly into the console. Entries in the interpreter are with; ; complete, which also multiline entries are possible. After compiling F # Interactive executes the code and writes the signature of all types and values compiled in the console window. Error messages are output.
Example
The following examples give "Hello World" from.
Let main = System.Console.WriteLine (" Hello World") or
Printfn "Hello World" The following function implements the recursive Ackermann function:
Let rec ack m, n = if m = 0 then n 1 else if n = 0 then ack (m - 1 ) 1 else ACK ( m - 1 ) (ACK m (n - 1)) Web Links
- Microsoft F # Developer Center on MSDN (English)
- F # from Microsoft Research Homepage (English)
- The F # Survival Guide (English)