F Sharp (programming language)

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

Template:Pound

F#
Paradigm multi-paradigm: functional, imperative, object-oriented
Appeared in 2002 (2002)
Designed by Microsoft Research
Developer Microsoft Developer Division
Stable release 1.9.7.8 (October 18, 2009; 474517152 ago)
Typing discipline static, strong, inferred
Influenced by Objective Caml, C#, Haskell[1]
OS Cross-platform (.NET Framework, Mono)
License Microsoft Research Shared Source license agreement ("MSR-SSLA")
Website F# at Microsoft Research
Wikibooks logo F Sharp Programming at Wikibooks

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 as part of Visual Studio 2010.[2]

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.[3]

A n-tuple represents a collection of n values, n >= 0. The value n is the arity of the tuple. The type unit (cf. the Unit type) corresponds to the 0-tuple and its only value is () which conveys no information. The type unit is used to implement functions that need no input and/or return no value. The 3-tuple is represented as (A, B, C), where A, B, and C 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=42 } 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 [].

The other sort of algebraic data type mentioned, "discriminated unions," (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 = 
    | ConstructorX of string
    | ConstructorY 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.[3] Data types are created with the type keyword. F# uses the let keyword for binding type values to a name (variable).[3]

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.[3]

All functions in F# are instances of the function type, and are immutable as well.[3] 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.[3] Like other functional programming languages, F# allows function composition using the >> operator. Every statement in F#, including if statements and loops, is a composable expression with a definite return type as well.[3] 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.[4]

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).[3] F# also allows extending the syntax to support embedding custom domain-specific languages within the F# language itself.[3]

F# provides sequence expressions[5] 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 do if b < 15 then yield 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.[5]

Sequence expressions and computation expressions are also used for creating asynchronous workflows.[6] 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.[6] 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.[6]

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.

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#:

let rec factorial n =
    match n with
    | 0I -> 1I
    | _ -> n * factorial (n - 1I)
open List
(* print a list of numbers recursively *)
let rec printlist lst =
    if lst <> [] then
        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
(* 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 % 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
(* 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)
(* async workflows sample (parallel tasks) *)

(* very naive prime number detector *)
let is_prime (n:int) =
 let bound = int (System.Math.Sqrt(float n)) in
  {2 .. bound} |> 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.RunSynchronously
        |> Array.filter snd
        |> Array.map fst

(* run a test *)
primes 1000000 1002000
    |> Array.iter (printfn "%d")

See also

Notes

References

  • Syme, Don; Granicz, Adam; Cisternino, Antonio (2007), Expert F#, Apress 
  • Harrop, Jon (2008), F# for Scientists, John Wiley & Sons 
  • Pickering, Robert (2007), Foundations of F#, Apress 
  • Smith, Chris (2009), Programming F#, O'Reilly 
  • Petricek, Tomas (2009), Real World Functional Programming With Examples in F# and C#, Manning Publications 

External links


ar:إف شارب (لغة برمجة) da:F Sharp de:F-Sharp cs:F Sharp es:F Sharp fr:F Sharp ko:F 샤프 it:F Sharp ka:F Sharp ms:F sharp nl:F sharp ja:F Sharp pl:F Sharp pt:F Sharp ru:F Sharp sv:F Sharp th:ภาษาเอฟชาร์ป zh:F Sharp

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...