Reservation in Lisp

I started learning Lisp and wondered if all the excesses of doing a particular task are useful in several different ways? We are sure experienced Lisp programmers can answer this question.

To just give an example. We can create functions using two different methods.

(defun add2 (x) (+ x 2)) 

or

 (setf (symbol-function 'add2) #'(lambda (x) (+ x 2)) 

I understand that this provides the flexibility to achieve different goals. But the correct explanation of why all this redundancy can help me better understand the situation.

+4
source share
3 answers

The first form exists because defining functions is such a normal job that you need convenient syntax.

The second form exists because sometimes you want to do advanced things with macros that generate function definitions and much more.

If there weren’t defun , we could define functions with our second form, but no one would program in Lisp, because a simple task would be extremely difficult. Each programmer will create their own defun macro, incompatible with everyone else.

+15
source

If you look at DEFUN in existing implementations, this is much more than just a function definition. It records, for example, the location of the definition for the development environment (development environment), sets documentation, records type information, ...

Often Lisp provides a mechanism in terms of a functional interface. Typical use is then accomplished with a set of macros that provide a convenient interface and side effects in a development environment.

Sometimes with CLOS there is even an object-oriented implementation under a functional interface.

Then the image is as follows

 macros <- used by the programmer, convenient to use ^ | functions <- user interface to the implementation ^ | CLOS (classes, instances, generic functions) <- low-level extensible machine 
+2
source

What you are describing is basically a side effect of the fact that most of Lisp is written by itself. Thus, both high-level definitions should be used normally, and the low-level material behind it is available to the programmer.

Having low-level definitions available can be very good if you want to make advanced material that would otherwise be impossible, but you can usually think of it as an implementation detail.

+1
source

Source: https://habr.com/ru/post/1380399/


All Articles