2

I'm making a toy lisp interpreter with D and I don't know the theory of Lisp very well.

I was wondering if Lisp can implement basic arithmetic functions (+, -, ×, ÷) by itself. Most Lisp/Scheme dialects implemented it with the builtins of C, Java-like language and overload it as lisp code(duplicated implements?).

I want to write arithmetic functions to Lisp code purely. Is it possible?

4 Answers4

5

Unless you want to use Church numerals or the like, at some point you're going to have to get into the hardware arithmetic instructions (add, sub, mul, div) one way or another.

If going down the hardware instructions route, then depending on your Lisp implementation, it may be implemented using C code (especially for an interpreter-based implementation), or those instructions may be emitted directly (for a JIT compiler-based implementation).

If you're trying to be as first-principles as possible, you can implement multiplication and division using addition and subtraction instructions (in a pinch, you can implement them the same way you were taught to in school, though you're using word-sized digits—that is, for a 32-bit machine, each digit is base-4294967296 instead of base-10).

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
3

The very simple solution would always to use your host numeric tower, but I understand your desire to keep the primitives low. The result however is a language like the first LISPs which had a bad rep about performance.

As an alternative to Chris's Church numerals you can model numbers using lists. Eg. 1234 can be (+ 4 3 2 1). Now you either have a low numeric type as primitive or the digits you see are simply self evaluating symbols which your math functions know what are. If you have a low numeric type you can add a exponent so it becomes (+ 0 4 3 2 1) for 1234 and (+ 1 4 3 2 1) for 12340 and (+ -11 4 3 2 1) for 0.00000001234. All arithmetic would be list iterations using exactly the math you know from school. It's more effective than Church numerals and slightly more efficient and it's easier to print it and read it.

I have used this on my little lisp interpreter that only have lists and symbols.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
Sylwester
  • 47,942
  • 4
  • 47
  • 79
2

If you are interested in implementing bignums in Lisp I can recommend this series by André van Meulebrouck:

https://web.archive.org/web/20101208222557/http://www.mactech.com/articles/mactech/Vol.08/08.03/BigNums/index.html

The link above is the web.archive.org. For some reason the original link while still live shows no contents ( http://www.mactech.com/articles/mactech/vol.08/08.03/bignums/index.html )

soegaard
  • 30,661
  • 4
  • 57
  • 106
0

All of Peano arithmetic is based on three functions:

  • zero, represented in Lisps as zerop or zero? depending on dialect;
  • successor, represented in Lisps by add1; if your dialect doesn't have it you're going to have to implement + in your bootstrap language anyway;
  • identity, represented in Lisp by equal.

With those three functions you can build the whole of arithmetic, but it is not going to be easy!

In my opinion you would be wise to build your Lisp arithmetic primitives (add, subtract, multiply, divide) in your implementation language. This is particularly so if you want to have first class ratios and bignums.

Simon Brooke
  • 162
  • 2
  • 8