Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LISP arithmetics implementation

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?


1 Answers

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).

like image 113
Chris Jester-Young Avatar answered Apr 18 '26 09:04

Chris Jester-Young