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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

4
(defun A (n) 
  (list 'the 'answer 'is n))
(A 4)
=> (the answer is 4)
Peter Lundgren
  • 8,787
  • 2
  • 26
  • 21
  • 5
    Personally I prefer quasiquotes for this: `\`(the answer is ,n)`. – C. K. Young Oct 08 '12 at 18:28
  • 1
    @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?](http://stackoverflow.com/questions/20207041/why-does-this-mapcan-cause-my-repl-to-freeze) – Menschenkindlein Dec 06 '13 at 16:40
  • @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. – C. K. Young Dec 06 '13 at 17:07
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.

user2240219
  • 166
  • 1
  • 2
  • 2