function include in LISP

Asked

Viewed 218 times

3

I’m having doubts about how to make a feature include in LISP programming. I can name someone, but I can’t include any additional information.

The function will include additional information of a person in a list. Follow the code:

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

This include I’m not able to do.

  • I don’t understand your doubt, what this function would do, is there an example? Post also the code you already made, so you can get a better idea of the problem (suggest [Edit] the question with the code).

  • So what is this list, how is it represented, where is it stored? And these "additional information" are what, is a fixed set of properties, are arbitrary properties? That’s why it helps if you post your current code and an example of what you want.

  • the function will include additional information of a person in a list. Follow the code: (setq AGENDA 'nil) (setq AGENDA ( include AGENDA '(Isabel 3233876))) this include am not able to do.

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, using:

(a b c d)

Or explicitly, using .:

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

So if your list AGENDA contains nil, and you want to add an element in 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’s left then is to turn it into a function:

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

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

Example in the ideone. P.S. I have little experience with Lisp, surely there is a simpler way to create these lists than with backtick/comma, but I don’t remember...

  • 1

    You can use the cons function, which you insert at the beginning of a list. (setf agenda (cons '("Isabel" 3233876) agenda)) (setq also works, but as far as I know setf is more common.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.