+5  A: 

Use the built-in "+" or "-" functions, or their shorthand "1+" or "1-", if you just want to use the result, without modifying the original number (the argument). If you do want to modify the original place (containing a number), then use the built-in "incf" or "decf" functions.

Using the addition operator:

(setf num 41)
(+ 1 num)   ; returns 42, does not modify num
(+ num 1)   ; returns 42, does not modify num
(- num 1)   ; returns 40, does not modify num
(- 1 num)   ; NOTE: returns -40, since a - b is not the same as  b - a

Or, if you prefer, you could use the following short-hand:

(1+ num)    ; returns 42, does not modify num.
(1- num)    ; returns 40, does not modify num. 

Note that the Common Lisp specification defines the above two forms to be equivalent in meaning, and suggests that implementations make them equivalent in performance. While this is a suggestion, according to Lisp experts, any "self-respecting" implementation should see no performance difference.

If you wanted to update num (not just get 1 + its value), then use "incf":

(setf num 41)
(incf num)  ; returns 42, and num is now 42.

(setf num 41)
(decf num)  ; returns 40, and num is now 40.

(incf 41)   ; FAIL! Can't modify a literal

NOTE:

You can also use incf/decf to increment (decrement) by more than 1 unit:

(setf foo 40)
(incf foo 2.5)  ; returns 42.5, and foo is now 42.5

For more information, see the Common Lisp Hyperspec: 1+ incf/decf

Jabavu Adams
Is it just me or does this seem to be a badge grab (self-learner)? Asking a question and returning a standard answer just two minutes later? Too bad you need three up votes on that answer...
froeschli
IMHO, this is what Stack Overflow should be: a careful answer to a very well-defined question. I'm not trying to game the system. If SO isn't the right place for this kind of stuff, then I'll put it on my own site.
Jabavu Adams
For instance, I was reading another SO page, where the poster asked a different question, but from his code snippet, it was clear he didn't know how to increment. I couldn't find that information. A lot of Lisp questions are from n00bs who want to try it out, so why not help them?
Jabavu Adams
@Jabavu Adams, Stack Overflow is for real questions that you can't answer yourself. You should also not try to invent questions and answers on random topics.
Rainer Joswig
Well, okay, if that turns out to be the community consensus; I'm new here. Still, in the interest of reducing the proliferation of bad n00b code, there should be some standard places to look for standard techniques. I thought that was what this site was supposed to be. P.S. What if these snippets were machine-readable?
Jabavu Adams
We should wait to see if Skeet has a better answer....
Glennular
@Rainer: no it isn't, check the FAQ: **It's also perfectly fine to ask and answer your own question, as long as you pretend you're on Jeopardy: phrase it in the form of a question.**
Otto Allmendinger
@Otto Allmendinger: I don't believe it was a question. If he has a real question, then finds out the answer, and answers his own question, fine. But inventing random trivialities and answering that with trivialities and even getting things wrong does not help anybody.
Rainer Joswig
@Rainer: Please, if you can spare the time, tell me where I got something wrong. I had to re-edit the page, because my comments in the sample code were misleading, and I hadn't executed the sample code before posting, if that's what you're referring to.
Jabavu Adams
@Rainer: To see the other side of the argument, you have to assume for a moment that the question is not trivial, although it may possibly be inappropriate for this community. It's extremely common for experts to have a distorted idea of what a beginner considers trivial or not. We all do it. If you continue to assume apriori that the question is trivial, then of course we're just talking past each other. I claim that there is a large set of people new to Lisp, for whom this question is (surprisingly!) non-trivial.
Jabavu Adams
@Jabavu, I suspect part of the problem here is people thinking you are "gaming" the system. Perhaps instead you should post the question and leave some time to allow others to answer it first. A question like this (certainly trivial to at least some portion of SO's userbase) would almost certainly attract quick answers. Then after an hour or so, if you felt anything was missing from what others had provided you could add your own answer. This way the outcome would be the same (a page for knowledge and substance) but without the negativity. Just my suggestion.
Simon P Stevens
@Simon, good advice. I considered this initially, but wanted to curtail rambling -- to create a concise answer to a concise, easily-searchable question. A lot of people don't scroll down a wall of text -- a good, useable answer should be up top. Perhaps this was arrogant of me. From a purely pragmatic perspective, anyway, it seems to have backfired. I *do* think there's an unserved need, here, though.
Jabavu Adams
@Jabavu Adams. Why not just make the question community wiki and then we all can go on, comfortable in the knowledge that this question is for the betterment of the community and not a badge grab.
chollida
@Jabavu Adams: Numbers are added with + and subtracted with -. That's all. There is nothing more to it. There is no need to use 1+, 1-, DECF or INCF. They are only shorthand notations. They are not more efficient or 'optimized for numbers'. All arithmetic operations are 'optimized for numbers', so that claim is misleading. Using the generic addition and subtraction is perfectly fine, there is no reason why one 'shouldn't use them'.
Rainer Joswig
@Jabavu Adams: Also, changing 'places' has nothing to to with adding or subtracting numbers. Changing values of places is a generic operation. INCF is just a shorthand for incrementing a place, MULTF already does not exist by default. You mention 'place', but THAT would be something worth to explain, because that's a thing that's special to Common Lisp. Addition of numbers is also special to Common Lisp, but you failed to even mention anything about addition that is special in Common Lisp about it (like for example that it is a generic operation for integers, ratios, floats, complex numbers).
Rainer Joswig
@Jabavu Adams: from the Hyperspec: " (1- number) == (- number 1)Implementors are encouraged to make the performance of both the previous expressions be the same." Common Lisp is defined such that nobody is forced to think that 1- will be in some way more efficient than a normal -. Any self-respecting Lisp compiler will show no difference in speed. Also - and + are also only defined for numbers. There is no difference to 1- or 1+. If somebody decides to write code or to generate code (for example via macros) that uses (+ x 1), then this is perfectly fine and there is no reason to change that.
Rainer Joswig
@Rainer: Thanks. I misread the spec. How embarrassing. I guess this was a valid question, after all. :)
Jabavu Adams
@Jabavu "A lot of people don't scroll down a wall of text -- a good, useable answer should be up top" - that's how SO works already, naturally, by design.
Skilldrick