Scheme 4 Javaer-4.Pair, car And cdr, javaer

Source: Internet
Author: User

Scheme 4 Javaer-4.Pair, car And cdr, javaer

In Java, all types except the basic type belong to the class/interface type. From the perspective of data abstraction, the basic types are more suitable for the subject, because the basic types have effective interfaces and implementations separated from the Java Virtual Machine specifications and implementation level. For data abstraction of classes and interfaces, we generally introduce concepts such as encapsulation, information hiding, and class interfaces. They describe Java classes as data abstraction. What users need to know is class interfaces.

In Scheme, we can construct a new data type from the basic type. In other words, we canNaked observation of Implementation Details of New Data Types(Unlike Java, the Implementation Details of basic types are completed by the Java Virtual Machine implementation vendor. The custom types of programmers depend on the class/interface definition method, therefore, this part of the content is a good supplement to the introduction to programming (Java) Chapter 4th data abstraction ).

All data constructed by Scheme from a forward pair is called list-structured data. Therefore, Scheme data can be divided into two types:Basic type and list.

Scheme:The two data are bonded together.. Now let's look at this adhesive 502.

Pair

If you put two things together, it does not indicate the relationship between the two. For example, modify the score/rational number code defined in introduction to programming (Java) · 1. 2.5 Case: score as follows.

Public class X {private int a; // numerator private int B = 1; // denominator public int getA () {return a;} public int getB () {return B ;}}
How do you know whether X is a score, a plural number, or something else? The type meaning of X, and its operation interface(The addition of scores or the addition of plural values.

Scheme defines the general structure of the new type called pair (pair,Sequential Pairs-- Sequence translation, I accidentally think of sequence Dolls), hereRegardless of the actual meaning of the Data Type and its interfaceBut simply look at how "two, three, three, everything ".

Three general functions related to pair.

L constructors (constructors) cons, put two things together.
(Define x (cons 1 2 ))

L select Sub (selectors, select function)Car And cdr,You can think of car as getOne And cdr as getTwo.

(Car x)

→ 1

(Cdr x)

→ 2

(The name cons stands for ''construct. ''the names car and cdr derivefrom The original implementation of Lisp on the IBM 704. car stands for ''Contents of Address part of Register'' and cdr (pronounced ''could-er '') stands for'' Contents of Decrement part of Register. '')

You can use pointers to understand the order (cons 1 2 ). Here there are three pointers, x or (cons1 2) as a pointer, pointing to a two-unit box. In the two units, the former stores the pointer car, and the latter is the cdr.

Currently, the space pointed to by the car stores a number 1. If the car or/and cdr point to the space, save a pointer pointing to the order to form a more complex data whole (belonging to a certain data type, forming a certain data structure ). Closure (closure)

It comes from the concept of abstract algebra. closure is used to describe the nature of a set (like the properties of a certain item, we do not consider another meaning of it-behavior with data ).

"Daosheng 1, life 2, LIFE 3, LIFE 3 ". There is a premise that all derived things belong to a set. For example, if a child in life is also a person, they can continue to have children. The result of an integer addition is an integer. So,Closure (closure)It indicates that a set meets one condition: the result of an operation on the set element belongs to the Set (so you can operate on it again ).

A set of closures with the powerful capabilities of "Three life. As a general construction block, cons can evolve the most suitable, useful, and complex types of data in this construction mode-for example, data types that describe various structures in data structure.
Sequence

A sequence is a set of ordered data, for example, {1 2 3 4 }. In Java, arrays or linked lists are used to describe/save sequences.

Assume that the car pointing to each sequence object saves a number and the cdr stores a sequence Object Pointer. In this way, each sequence object is like a one-way linked list.Node. Note: The last sequence contains a nil (null Latin) indicating the end of the sequence ).

(Cons 1 (cons 2 (cons 3 (cons 4 nil ))))

This simple Composite data/data structure is very common and Scheme provides a cheap OperatorListTo make it easier for programmers to represent the above expressions.

(List 1 2 3 4)

→ (1 2 3 4)

(List 1 (list 2 3 4 ))

→ (1 (2 3 4 ))

(Cons 1 (list 2 3 4 ))

→ (1 2 3 4)

(Cons (list 1 2) (list 34 ))

→ (1 2) 3 4)

(List 1 2) (list 34); use a tree to explain this expression

→ (1 2) (3 4 ))

 

It is worth noting that there is an interesting relationship between data types and data structures.

(Define x (cons 12). We don't know what data type x is;

(Define x (list 12), or (define x (cons1 (cons 2 nil )))

We know that x is a list.
Symbol data

Suppose (define x (cons 1 2) is defined as a score of 1/2 (not a plural number). As a general application, mathematicians want to Use algebraic symbols, for example, (define x (cons a B) indicates a score a/B. Here, a and B represent any number.

In the following example, although the score a/B is defined, a and B are not symbols representing any number.

(Define a 1)

(Define B 2)

(Define x (cons a B ))

Scheme providesPrefix quotation marksFor example, 'a indicates that a is a symbol.

(List 'A' B 'c)

→ (A B c)

Next, let's learn what we will do after bonding. For example, (1) How is the score or plural number?Its operation interface depicts its type;

(2) how to operate and apply list...

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.