F # Learning Road (1) What is functional programming

Source: Internet
Author: User
Tags definition constant ocaml

For what is functional programming, this is the parrot. This article does not intend to define this, but the hope and garden friends to discuss this topic, throwing bricks only for the jade.

1, Wikipedia gives the definition is:

Functional programming is a programming paradigm that treats computer operations as computations of functions. The most important foundation of a functional programming language is the lambda calculus (lambda calculus).

Furthermore, the functions of the lambda calculus can accept functions as input (parameters) and outputs (return values).

2, the function should be higher order function, can Corrie

As you can see from the Wikipedia definition, there is no objection to the notion that functions are first-class citizens (the class one) in functional programming languages.

The function should enjoy equal treatment of the value, which can be used as a function parameter and can be used as the return value of a function.

function should be a higher order function. The function should be currying.

#light
let p f=
fun (x)-> f(x)
let c t=
t+10
let r=p c
let f=p (fun x-> "Hello," + x )
printfn "r=%d" (r 10 );
printfn "f=%s" (f "Lvxuwen")
open System
Console.ReadKey(true)

Define a function in F # using let-binding expressions, the syntax rules are as follows:

Let function name parameter 1 parameter 2 ... Parameter n= function body

F # parameters are separated by a space, unlike C # separated by commas, and the arguments are not in parentheses.

The first line of the F # code above uses the #light instruction, pointing to the use of a streamlined grammar rule, rather than using the OCaml syntax, where the OCAML syntax uses the begin end;; ; In keyword to differentiate between statements and scopes, after using the #light directive, although we can use concise syntax, it also means that F # will use indentation to differentiate between statements and scopes. You can select Tools in VSS-Options-Text editor->f#-tabs, set tab to insert Spaces, which allows you to easily indent using the TAB key. In addition, in VSS, you change the previous example "Hello," to Chinese "welcome", if garbled in the console, you can choose File-> advanced save option-> change character encoding to Chinese Simplified GB2312 or Unicode encoding.

3, pure functional language without variables and side effects

This is an important reason why functional languages are considered to be the most ideal language for concurrent programs. Without variables and side effects, there is no longer a problem of competing resources, and locks are no longer needed.

In F #, the identifier for a let definition is an expression and cannot be changed once defined. To define a variable, use a specific keyword mutable

let mutable var=20
var<-30;
let constant=30
constant=40
printfn "var=%d" var
printfn "constant=%d" constant

Variable var uses the Mutable keyword definition and assigns values using the <-operator. While the = operator is used as the equality comparison operator, and constant uses <-, it compiles an error

In functional programming, functions are used to define rules, algorithms, and if a function is passed in with the same value and always returns the same result, we say that the function has no side effects, idempotent. Such functions are most consistent with the principle of reuse and can be predicted in higher-order function computations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.