Getting started with racket functional programming

Source: Internet
Author: User

I always wanted to learn lisp. Today I finally started my career. Nowadays, learning lisp can be used for practical projects, rather than simply learning functional thinking methods, so as to gain a deeper understanding of the nature of computing, it can better use C ++, Java, Python, and other coding methods. What's more, these mainstream languages have gradually added functional programming features. c ++ and Java have now introduced lambda expressions. Let's assume that we can systematically learn lisp, and believe that we will master the new features of these languages and write JavaScript and python for ourselves, it is helpful to understand closures, high-order functions, and lambda expressions. To get down to the truth, we recommend three resources:

  • Racket Official Site
  • How to design programs
  • How to design a program (How to design programsChinese Version)
The Birth and Development of racket

Brief Introduction to the Development of racket. For details, refer to zhihu's answer to a racket question:
In 1958, John McCarthy, the father of AI, invented a Lambda algorithm-based symbol processing language. In 1960, McCarthy published a famous paper.Recursive functions of symbolic expressions and their computation by MachineThis language is named lsip (list processor), and its syntax is named: Symbol Expression (S-expression ). LISP is built on Seven functions [atom car CDR cond cons EQ quote] and two special [Lambda labels.


Lisp was born for pure scientific research. Code runs like a mathematical formula and is computed by human brains. The history of lisp as a computer language was not started until the eval function was implemented on the IBM 704 machine by Stephen Russell, a member of the company. In 1962, the first complete Lisp compiler was born at MIT. Since then, LISP has spread around the world with MIT as the center. Over the next 10 years, various lisp dialects have emerged.


In 1975, scheme was born. Scheme is born with MIT. Its Design Philosophy is minimal minimalism. It only provides a few required primitives, and all other useful functions are implemented by libraries. With minimalism, scheme tends to be extremely elegant and is widely used in the education field as a computer language.


In 1984, Common Lisp was born. In the 70 or 80 s of the 20th century, due to the excessive number of Lisp dialects and the split of communities, it was not conducive to the overall development of lisp. Beginning in 1981, a lisp hacker organization launched Common Lisp in 1984 after three years of efforts and integration. Because the design concept of scheme is different from that of other lisp versions, although Common Lisp draws on some features of scheme, it does not integrate scheme. Since then, LISP has only two dialects: Common LISP and scheme.


Since 1990s, LISP has gradually declined due to the rise of C ++, Java, and C. After 2005, with the rise of scientific computing, the popularity of dynamic languages JavaScript, Python, and Ruby, LISP gradually came back to people's eyes. In the traditional education field of lisp, Python, as a powerful challenger, initiated a charge against scheme. In 2008, MIT gave up the use of scheme as the teaching language (the construction and interpretation of computer programs) course, and enable python for basic teaching. At the same time, Northeastern University set up another stove, and its leading scientific computing system PLT scheme began to develop rapidly. In 2010, PLT scheme was renamed racket. In recent years, the racket language has continuously become the most active language site of the year, along with Haskell.


Symbol Expression S-expression

First, let's talk about the s expression: the basic elements of the S-expression are list and atom. A list is surrounded by parentheses and contains any number of elements separated by spaces. atoms are other content. It uses prefix notation for both code and data in lisp. For example, 1 + 2*3 is written as a prefix expression (+ 1 (* 2 3 )).

  • Strengths: easyparse is simple and pure, so you don't need to consider priorities. It is also a prerequisite for code as data;
  • Disadvantage: the readability is not very strong;
High-order functions

A higher-order function must meet at least one of the following conditions:

  1. Accepts one or more functions as input;
  2. Output a function;

The derivative in calculus is an example of ing a function to another function. In non-type lambda calculus, all functions are of higher order. In functional programming, a higher-order function that returns a function is called a curry function. Curry converts a function that accepts multiple counts into a function that accepts a single counts (the first counts of the original function, and return the technology of the new function that accepts the remaining number of workers and returns the result. For example, f (x, y) = x + y. If y = 1 is given, the function g (x) = x + 1 is obtained.


Lambda expressions

In racket, lambda expressions are used to define anonymous functions. The usage principle in "how to design a program" is as follows: Assume that a non-recursive function only needs to be used once when the number of arguments is used, and lambda expressions are used. If you want to use a Lambda expression to express recursion, You need to introduce the y combination sub-operator. Y is such an operator that acts on function f no matter what one (accepts a function as the number of arguments, returns a Function x. Then, apply F to this function x and obtain X. Therefore, X is called the fixed point of F, that is, (y f) = (f (y f )).


Evaluate inertia

Lazy evaluation (lazy evaluation). To put it bluntly, some intermediate results do not need to be obtained, but it is not conducive to the subsequent computation. For details, see inertia evaluation and inertia programming.
Inertia is a concept in computer programming. It aims to minimize the work that computers need to do. The most important advantage of inert computing is that it can construct an infinite data type. When using the inertia evaluation, the expression is evaluated not immediately after it is bound to a variable, but when the value is used. Statement such as X: = expression; (assign the result of an expression to a variable) call this expression and place the calculation result in X, however, no matter what is actually in X, until the value needs to be referenced by X in the following expression, the value of the subsequent expressions can also be delayed. Finally, this fast-growing dependency tree is computed to generate a symbol that is visible to the outside world.


Closure

In computer science, closure refers to lexical closure, a function that references free variables. A free variable is a symbol used to represent a position or some positions in an expression. For example, if f (x, y) is used to evaluate the deviation of X, Y is a free variable. This referenced free variable will exist with this function, even if it has left the environment where it was created. When a function is defined as a (nested) function, a closure may be generated if the internal function references the variable of an external function. During execution, once an external function is executed, a closure is formed, including the code of the internal function and the reference of the variables in the required external function. The referenced variable is called the upper value (upvalue ). There are many articles about JavaScript closures on the Internet. If you have a systematic understanding of lisp, this concept will naturally be very clear.


Fast racket implementation
# Lang racket (define (quick-Sort array) (cond [(empty? Array) Empty]; the idea of quick sort is grouping + recursion [else (append (quick-sort (filter (lambda (x) (<X (first array ))) array); here the array is the closure (filter (lambda (x) (= x (first array) (quick-sort (filter (lambda (x) (> X (first array)]) (quick-Sort '(1 3 2 5 3 4 5 5 0 9 82 4 ));; execution result '(0 1 2 3 3 4 4 5 5 9 82)

Through this example, we can feel the powerful expression ability of racket language based on Lambda operators. The use of high-order functions, lambda expressions, and closures makes the fast sorting described by racket very refined, which is totally different from the C language based on the von noriman model. Later, as racket learns more deeply, I will try to write down the interpreter.

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.