Implementation of Lisp list and operations in Javascript

Source: Internet
Author: User

In Lisp, a list is a value pair. You can operate cons to create a value pair. For example, cons 1 2 and 1 and 2 are two values of the value pair. Cons operations are closures, so the elements that constitute the list can be atomic or list types, such as cons 1 (cons 2 3 )). The operations for reading the list include car And cdr, which are the "Left value" and "right value" of the read value pair, for example (car '(1 2), return 1, (cdr '(1 2) returns 2, and the car And cdr operations are also closure.

Most programming languages can easily implement this function by having iterative operations (for | while) to describe whether an element is included in the list of programs. But Lisp can use recursion to complete iteration, and I am surprised that it only uses car And cdr.

(defun our-member (obj lst)   (if (null lst)       nil   (if (eql (car lst) obj)       lst       (our-member obj (cdr lst)))))

This pure function is more concise than the procedural for & while syntax. Before learning the lisp language, I never thought about traversing a list. In addition to iteration, recursion can be used, which is more suitable for describing a mathematical computing problem.


For a language like Javascript, it has a richer data structure than lisp. Generally, we use an Array to represent the list. Its syntax is more intuitive:

var list =[1,2,3,4]; for(var i =0;i< list.length;i++){     //list[i]}

Even so, I still hope that Javascript can only use the cons, car, And cdr functions to complete various list operations, so I would like to think about different languages, this is why I wrote this note.

Javascript pure function implementation cons, car, cdr

Var cons = function (left, right) {return function (f) {return f (left, right) ;}; // return the list function to left, right is always saved in memory} var car = function (list) {return list (function (left, right) {return left ;}); // instantiate the high-order function f, take the left value} var cdr = function (list) {return list (function (left, right) {return right ;}); // instantiate a high-order function, right Value} var list = cons (1, cons (2, 3); car (list); // 1car (cdr (list); // 2

Instead of using any javascript data type to store left and right values, cons returns a "list function". With the help of Javascript closure, cons transmits left and right to another high-order function f, so that external functions can access them. Finally, in order to get the left and right of the list, we only need to instantiate the high-order function f in car And cdr and pass it to the "list function" list.

Conclusion: Where is the data? Through closure, Javascript allows higher-order functions to accept free variables as parameters. Free variables are generated and eliminated with higher-order functions, but are higher-order functions not used as data when being called? As declared by functional languages, everything is a function.

Note: Javascript closure is usually called a closure, but it is different from the closure operation mentioned above.


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.