3

I was in #lisp on freenode recently, and someone mentioned the existence of '1-' and 1+. Knowing about these functions left me wondering why they exist. Were they originally created for perormance reasons like the related -- and ++ of C/C++, or was there some other reason? Does anyone know the history of how these functions came to be in the standard?

(If this question is more suitable for another site (e.g. Programmers) please migrate it there. Thanks.)

Faheem Mitha
  • 6,096
  • 7
  • 48
  • 83
  • 4
    1+ and 1- are just convenience functions. They have been forever in Lisp. In early times they were called ADD1 and SUB1. – Rainer Joswig Sep 19 '14 at 09:56
  • 1
    This is just a guess, but maybe it did originally have to do with performance, the reason I say this is my experience looking at the '+ and '1+ implementations in emacs lisp, '+ and other math operators is implemented in a slower generic way that involves quite a few more checks and function calls, while '1+ and '1- are implemented very differently and they are actually quite a bit faster. in emacs lisp (+ 1 1) does very different things than (1+ 1). In fact in my tests (1+ 1) ran 40% faster than (+ 1 1). I have no idea how popular CL implementations do it, but perhaps it is similar. – Jordon Biondo Sep 23 '14 at 19:02

1 Answers1

6

I don't think there are performance reasons for them in Common Lisp (although in C, for example, n++ may work faster than n = n + 1). There is a note at the bottom of your link:

(1+ number) ==  (+ number 1)
(1- number) ==  (- number 1)

So, generally one can use + or 1+ with the same result. I think that the real reason for existence of these functions is convenience. Personally, I found myself adding 1 more often than any other number.

Funcions of increment and decrement can be found almost in every programming language (as far as I can tell), so it is rather logical that there are some in Common Lisp.

P.S. I doubt that it is really on topic and that my answer is useful, so it's community wiki ;-)

Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
  • Maybe there is no good answer to this question, in which case I can delete it. And no, not all programming languages have increment/decrement operators. The bottom of http://en.wikipedia.org/wiki/Increment_and_decrement_operators has a list. For example Pyth0n does not. – Faheem Mitha Sep 19 '14 at 09:23
  • Also note that C language can increment variable in memory (or register) and it can do it really fast, but Common Lisp just returns new value, it is by definition not as fast as modifying 'in place'. – Mark Karpov Sep 19 '14 at 09:25
  • Right. Also, clojure doesn't seem to have them either. – Faheem Mitha Sep 19 '14 at 09:26
  • @FaheemMitha, I can tell you for sure that there are `dec` and `inc` in Clojure. – Mark Karpov Sep 19 '14 at 09:29
  • 7
    @Faheem Mitha: 1+ and 1- and not increment and decrement operators like the Wikipedia page describes. In Common Lisp these are INCF and DECF. – Rainer Joswig Sep 19 '14 at 09:48
  • @RainerJoswig I see. How do these differ? If you think this question should be deleted, close or moved to another site, let me know. Maybe it has no good answer. PS. Ok. I see INCF/DECF increment/decrement in place, instead of returning a value like 1+/1-. – Faheem Mitha Sep 19 '14 at 09:56
  • 1
    @FaheemMitha If you have a variable `foo`, wit hthe value 17, after `(1+ foo)` (this has the value 18), `foo` is still 17. However, after `(incf foo)` (the value of this expression is still 18) `foo` now has the value 18. – Vatine Sep 19 '14 at 11:18
  • 1
    @FaheemMitha To reiterate what Vatine said, it's the same as the difference between `n + 1`and `++n`. After the first, `n` still has the same value, but after the second it doesn't. To extend on what Vatine said, `incf` (and `decf`) also take an optional `delta` argument which specifies how much to add. So `(incf n)` is like `++n`, but even more like `n += 1`, because `(incf n 3)` is just like `n += 3`. – Joshua Taylor Sep 19 '14 at 11:47
  • @Mark most (all?) CL implementations store integer less than half the range for a machine word in the pointer itself and if you recurse or loop the optimized will update the same stack address if it sees one goes out of scope while the next goes in. These are implementation details and not part of the spec but there seems to be consensus on it. – Sylwester Sep 19 '14 at 11:49