Polynomial addition--c++ and racket

Source: Internet
Author: User

The addition of polynomial is a simple to explode algorithm exercise, after learning the chain list will generally be the wave pull to discuss the sparse, dense with what to express, and finally generally as a list of exercises appear. In fact, using arrays to represent the polynomial is obviously unreasonable, most of the polynomial must not be so close, the list is almost the only choice.

In a language such as C + +, directly build a Poly node class, and then build a poly class (including a head node), basically is a single-linked list implementation, for the addition of the polynomial, and the chain of ordered linked list more like, multiplication is more simple, one by one by the past on the line.

The reason this process is simple is that the encapsulation of the class (C with the struct and some functions for this simple problem is basically no different), of course, a map can also simulate this structure.

when we choose racket to achieve the problem is much more interesting , and, all the hierarchy of the implementation of the package itself, the approximate process is: from the polynomial node term construction, to the polynomial list terms, and finally terms and a variable (for example, X Cons together , each step to do a good package, more useless, this is a worthwhile project, the important thing is not the polynomial itself, but from the experience of encapsulation and abstraction of the skills and meaning.

In the racket language, the syntax is very simple to construct an unusually complex data structure and functions, so that there is a strong expression, of course, the designer's abstract packaging skills, otherwise the code is a bit larger than the number of bugs can not be seen.

The code is not posted here, SICP the second chapter at the end of this example, and there are rational numbers system, symbolic derivation and other examples, are a good example of understanding the nature of closures and data abstraction, strongly recommended.

(If you have not learned scheme, it is very good to start directly from SICP, because it will only give you a little bit of grammar to create complex structures, only to continue to introduce you to the grammar, the process of grammar is to serve the mind, So there is absolutely no need to first go to the scheme grammar and then see.

Polynomial addition--c++ and racket

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.