4

I am trying to write a function lets say A(n)

which is supposed to have a list

(the answer is n)

n being any integer in the list I want when i type (A 4)

it should display (the answer is 4)

I am not sure how to go about it thinking of using setq and the list function

But how to construct it its confusing me I'm just a newbie trying to learn lisp, any ideas or books I can read I will greatly appreciate.

2 Answers 2

4
(defun A (n) 
  (list 'the 'answer 'is n))
(A 4)
=> (the answer is 4)
Sign up to request clarification or add additional context in comments.

3 Comments

Personally I prefer quasiquotes for this: `(the answer is ,n).
@ChrisJester-Young It must be mentioned that in this way fuction can return literal conses. For example, if code is `(,n is what the answer is) And this can cause non obvious bugs. See Why does this mapcan cause my REPL to freeze?
@Menschenkindlein Of course, the same caveats apply as when deciding whether to use list/cons or literals. Since I'm a Schemer, my habit is to return immutable data by default, and only enabling mutability on a case-by-case basis. Certainly I do not mutate objects returned by other functions unless it's expressly documented as being a fresh instance or otherwise okay to mutate.
0

Another part of your question is what books to read. Usually people recommend to read the "Practical Common Lisp", a book with a friendly and easy-to-read introduction to the Common Lisp. Then there is the "Getting Started" article on Cliki.net. This should be sufficient to get started with the language.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.