include function in LISP

3

I'm having second thoughts on how to do an INCLINE function in LISP programming. I can name someone, but I can not add some additional information.

The role will include additional information for a person in a list. Follow the code:

(setq AGENDA 'nil)
(setq AGENDA ( incluir AGENDA '(Isabel 3233876)))

This include I can not do.

    
asked by anonymous 22.06.2015 / 16:11

1 answer

2

In Lisp, a list has two parts: the "head" ( car ) and the "tail" ( cdr ). The head is a common element of the list, and the tail is the rest of the list (or nil , if the list is over). You can create a list implicitly by using:

(a b c d)

Or explicitly, using . :

(a . (b . (c . (d . nil))))

So, if your AGENDA list contains nil , and you want to add an element to it, replace it with a new list, with the new element in the head and the old element in the tail:

(setq AGENDA '(("Isabel" 3233876) . ,AGENDA))

All that remains is to turn it into a function:

(defun incluir (lista elemento) '(,elemento . ,lista))

(setq AGENDA (incluir AGENDA '("Isabel" 3233876)))

Example in ideone . Q. I have little experience with Lisp, there is certainly a simpler way of creating these lists without being backtick / comma, but I do not remember ...

    
22.06.2015 / 16:37