I thought the functional programming

Source: Internet
Author: User
Tags setf

Function-Type programming

The idea of functional programming (functional programming) is relative to imperative programming (imperative programming), telling the computer what you want rather than telling it what to do, for example:

(Defun fun (x)   (List ' A (EXPT (car x) 2))

This is the function that is programmed, and

(Defun imp (x)   (Let* (Y (Car x))         (Z (expt y 2)))     (list ' a z))
Is imperative programming, the result is the same, but the thinking is different.

Functional programming also avoids modifying the parameters of the function to avoid the so-called side effects (side effect), except where it is to be exploited in some places.
The C language can only return a value, so it is sometimes possible to pass a parameter as a return value with a pointer or reference, while Lisp can return multiple values

From bottom up

Bottom-up programming relative to the top-down, divided into the programming style, advantages: the use of CLISP Interactive programming environment, you can write a module immediately after the test; some utility functions (utility function) can also be used in future projects, As more programs are written, the more valuable these utility functions are, the richer your tools are, the more compact and ax the program is.

Abstract

To write utility functions, we need to develop abstract abilities and find common structure/pattern of similar programs, such as often writing recursion:
To find the length of a class table

(Defun len (LST)   (if (null LST)       0       (+ 1 (len (CDR lst))))
Determines whether all elements in the LST list are judged to be true by the FN function:
(Defun All (FN LST)   (if (null LST)       T       (and (funcall fn (car lst)) (all FN (CDR lst))))

(In fact, the above two examples of clisp have built-in functions, called length and every)
They are similar in structure, and can write a more general function to implement the function of this recursive structure:

(Defun rec (joiner end base)   (Labels (Inner-rec (LST) (if (null LST) (if (          Functionp end) (Funcall end) end)          (Funcall joiner                   (if (fun Ctionp base) (funcall base (car lst)) base)                   (Inner-rec (CDR lst)))))) (      # ' Inner-rec))

The function of the Rec return value is also a function, the returned function has a recursive structure, can implement the above Len and all two examples of the function:

(SETF all2 (rec # ' (Lambda (x y) (and x y) t # ' ODDP)) (Funcall all2 ' (1 2 3)), equivalent (all # ' ODDP ' (1 2 3)) (SETF len2 (rec # ' + 0 1)) (Funcall len2 ' (1 2 3)), equivalent (Len ' (1 2 3)) (SETF Co PY (Rec # ' cons Nil # ' identity); Generate a function copy (SETF has-number (rec # ' (Lambda (x y) (or x y)) Nil # ' Numberp) for copying the list; Has-number Award Whether a broken list contains numbers ...
Think recursively

The author uses recursion extensively in books, although sometimes not very efficient, and he thinks recursion makes code elegant. First pass a recursive write, the code is completed and then try to change to the tail recursion, so that the compiler (which is supposed to be reader) will optimize the tail recursion into a more efficient iteration


I thought the functional programming

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.