F-Sharp

from Wikipedia, the free encyclopedia
F #
Fsharp logo.png
Functional programming language
Basic data
Paradigms : multi-paradigmatic: functional , imperative , object-oriented
Publishing year: 2002
Designer: Don Syme, Microsoft Research
Developer: F Sharp Software Foundation , Don Syme , Microsoft
Current  version : 4.7   (September 23, 2019)
Current preliminary version: 5.0 preview 1   (March 18, 2020)
Typing : static , strong , implicit
Influenced by: Objective CAML , C # , Haskell
Affected: Elm , F * , LiveScript
Operating system : cross-platform
License : Apache license 2.0
F # Software Foundation

F # (Read: F sharp ; English pronunciation [ ɛfː ʃɑrp ]) is a type-safe multi- paradigm language with a strong focus on functional programming for the .NET framework. The syntax is very similar to that of OCaml , as this language served as a model for the implementation of all functional language elements and the imperative side of F #, which is derived from C # , is rarely used. In addition to these components, F # also contains object-oriented language constructs that fit into this language concept.

F # is fully interactive with all other languages ​​on the .NET platform and, thanks to .NET Core and Mono, offers the possibility of using the code on Windows as well as Linux, the BSD and illumos families and macOS . Android and iOS are also possible as target platforms via Xamarin .

Originally developed as a research project by Microsoft Research , F # is currently being carried out by Microsoft's development department and the F # Software Foundation. F # was officially supported and shipped for the first time as part of Visual Studio 2010. In November 2010, F # and associated compilers and libraries were released under the Apache License 2.0 .

Some language features

values

In predominantly imperative languages, variables are the primary language construct for storing values. This is done in functionally oriented programming languages ​​such as F # by means of "immutable" language constructs. Values ​​of a certain type can be declared with the frequently used keyword let .

let pi = 3.1415927
let name = "John"

F # provides type derivation , i.e. H. Types of expressions are determined automatically. For example, gets piautomatically the type of floating - literal assigned.

Changeable variables are mutablepossible with the keyword. These are then changed with the assignment operator <-.

let mutable x = 10
x <- 15

Changing values ​​afterwards is also possible through the use of so-called reference cells :

let x = ref 0     // x hat den Typ "int ref", ist also eine Referenz auf einen Integer
x := 5            // x wird ein neuer Wert zugewiesen
printfn "%i" !x   // Mittels des "!"-Operators wird x dereferenziert. Gibt 5 aus.

Functions

Functions are declared like other values letand can expect parameters:

let square x = x * x
let add x y = x + y

Functions can expect functions as parameters (see higher-order function ):

let do_twice f x = f (f x)

The types of parameters are recognized automatically, but can also be declared explicitly:

let add (x: int) (y: int) :int = x + y
let do_twice (f : int -> int) (x: int) = f (f x)

The instruction

printfn "%A" (do_twice square 5)

returns 625 (the square of the square of 5). do_twiceWith the help of the composition operator and after Eta reduction , the function can also be used as

let do_twice f = f >> f

to be written.

In the example above, squarethe type is determined for, that is, is a function that expects a parameter of the type and returns a value of the type . For you get the type . This means, is a function that receives a value of the type as the first parameter (a function with a parameter of the type and a return value of the type ). As a second parameter it receives a value of the type and it returns a value of the type . has the role of a type variable here (roughly comparable to generic or template parameters in Java / C ++ , see Polymorphism (programming) ). int -> intsquareintintdo_twice('a -> 'a) -> 'a -> 'ado_twice('a -> 'a)'a'a'a'a'a

In F #, parameters are passed to the function without brackets, etc., separated only by spaces. Only if the return value of another function is required as a parameter do brackets need to be set to define the evaluation order of the expressions. With the values ​​5 and 8 belong to the function ; its return value is a parameter for the function . printfn "%A" (add 5 8)addprintfn

F # enables closures and uses currying automatically:

let add x y = x + y
let inc = add 1

In the definition of inc, the first parameter of the function is addbound to the value 1. The result of this partial application of functions is a new function with only one parameter. The evaluation of the expression

inc 5

returns 6 as the result.

F # supports tuples :

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 # offers Discriminated Unions and Pattern Matching :

// Ein Element vom Typ Baum ist entweder ein "Ast" und enthält zwei Elemente vom Typ "Baum",
// oder es ist ein "Blatt" und enthält einen Integer
type Baum =
| Ast of Baum * Baum
| Blatt of int

let rec baumSumme x =
    match x with
    | Ast(l, r) -> baumSumme l + baumSumme r
    | Blatt(x)  -> x

There are also type providers for type-safe processing of external data with IntelliSense.

Object-oriented programming is also possible in F # . 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 ist %i Jahre alt." x.name x.age

Null pointers are only required for interaction with classes from the .NET framework.

syntax

Two forms of syntax are possible in F # code: simple syntax and verbose syntax. The simple syntax is used by default, but the indents are very important in this form. These play a smaller role in the detailed syntax, because keywords such as, for example begin, endand the inbeginning and end of code blocks determine there.

Example of the simple and detailed syntax:

simple syntax 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 case of the simple syntax, the indentations are mandatory; in the case of the detailed syntax, they could also be left out.

Development environment and compiler

F # code is compiled , creating intermediate code in the Common Intermediate Language (CIL), just like programs written in C # or VB.NET .

There is also an interactive environment or F # interpreter , F # Interactive or FSI for short. So you can write the code directly in the console . Entries in the interpreter must be completed with ;;, which also enables multi-line entries. Once compiled, F # Interactive runs the code and writes the signature of all compiled types and values ​​in the console window. Error messages are also output in this way.

This interpreter allows the execution of code in supported editors such as Visual Studio Code without prior compilation, as is known from dynamic languages.

Examples

The following examples output " Hello World ".

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))

literature

Web links

Individual evidence

  1. Announcing F # 4.7. September 23, 2019, accessed June 9, 2020 (American English).
  2. Announcing F # 5 preview March 1, 19, 2020, accessed June 9, 2020 (American English).
  3. F # to ship as part of Visual Studio 2010 . Don Syme's WebLog on the F # Language and Related Topics
  4. Type Provider
  5. Microsoft Verbose Syntax (F #)
  6. ^ Microsoft F # Interactive Reference