F # Adventure Tour (V): Understanding functional programming through F # (ON)

Source: Internet
Author: User
Tags anonymous cos

About functional Programming (functional PROGRAMMING,FP)

Functional Programming (FP) is a programming paradigm in which the computational process of a computer is considered a function operation to avoid modification of state and data. It emphasizes the use of functions more than imperative programming. The lambda operation builds on the basis of functional programming. Important functional programming languages include Lisp, Scheme, Erlang, Haskell, ML, and OCaml, and Microsoft introduced F # in 2005.

In addition, a number of languages, including C/c++/c#/python/javascript, also provide partial support for FP. From this we can draw a conclusion that the single programming paradigm is difficult to meet with the increase of the complexity of the real problem. We need to know more about FP and the question is what language to study? As one. NET programmer, my answer is F #. With F #, in addition to the power of FP, the most important thing is that it is with. NET platform seamless, it can easily interoperate with C # or vb.net, through F #, our hands of c#/vb. NET will become more powerful.

This article attempts to introduce some important features and attributes of FP through F #. This includes functions (first-class citizens, higher-order functions, Gerty, anonymous functions, closures), avoidance of side effects (modification of State and data), recursion, lazy evaluation, pattern matching, and then discusses the effect of FP on code structure. Like continuation and Monad to stay in later essays on the introduction. Hope to increase your knowledge of FP.

function is a first-class citizen (first-class citizen)

The citizen can also be used as object/value/entity, the so-called first-class citizens are those in the program can be unrestricted (compared to other objects in the same language) to use the object. In a programming language, "a function is a first-class citizen" means that it can:

1. Literal value expressed as anonymous

2. Stored in variables

3. Stored in the data structure

4. As a function of the parameters to pass

5. As the return value of the function

6. Construct at run time

A function in F # is a first-class citizen, whereas in C #, a function is not a first-class citizen, for example, we cannot pass a function as a parameter, nor do it as a return value, but a class can do so. This difference is not surprising. If we look at human society as an abstraction, then the level of citizenship in its different implementations is also different. In Burma, monks are first-class citizens, men are second-class citizens, women and nuns are third-class citizens, Simon is a four-class citizens, our country is obviously not the case, but the majority of Burmese and Chinese citizens can live well.

F# Code - First-class citizen
#light
let makeDerivative f (deltaX: float) =
  fun x -> (f(x + deltaX) - f(x)) / deltaX
let cos = makeDerivative sin 0.000001
open System
let writeLine input =
  print_any input
  Console.WriteLine()
writeLine (cos 0.0) // ~= 1
writeLine (cos(Math.PI / 2.0)) // ~= 0
Console.Read()

In this example, the first argument F of the makederivative function is a function, and its return value is also a function, which returns an anonymous function.

Higher-order functions (high-level function)

Higher-order functions are those that can accept other functions as arguments, or functions as return values. The Makederivative function above is an example. Higher-order functions describe the mathematical concepts of functions, whereas "functions are first-class citizens" is a computer science term.

Remember the concept of compound functions that you learned in high school math? if u (x) = x * 2, and y (U) = U + 3, then y accepts "argument" as a function, and y itself is also a function.

Related Article

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.