Skip links

Generating Polynomials – Part 2

In my last article, I showed how we can use Prolog to generate univariate Polynomials. After I finished that article, I wanted to try Lisp for the same task. Prolog is “declarative”, whereas Lisp is primarily “functional” (it supports OOP as well), so the implementation will exhibit that difference.

Here is the primary function generate-polynomial along with the tester function (main):

Function to Generate Polynomial
Function to Generate Polynomial

Here is sample output (polynomials of degree 4):

Sample Output
Sample Output

Generate-polynomial uses the following functions to generate a polynomial of given degree:

Core Functions
Core Functions

A few utility functions are also used along the way. These are:

Some Utility Functions
Some Utility Functions

Here is another sample output:

Another Test Run
Another Test Run

As you can see, the output is quite random. Let us try to generate polynomials of degree 3:

Polynomials of Degree 3
Polynomials of Degree 3

Nothing fancy or surprising here. The program works as expected.

Between the Prolog and Lisp implementations, I think had to write some extra code to get the Lisp implementation working. Maybe if I had used some external utility libraries, I could have saved some code, but that is no big deal. Anyway, it was a nice exercise for me!

The code is implemented in LispWorks Lisp 8.0.1 Enterprise Edition for Windows 64 bit. You can download the code here.

Have a nice weekend!

Leave a comment