In Lisp is the function `1+` just syntactic sugar? -
i started learning lisp. 1 of first concepts grasp seems prefix notation (i.e. instead of writing "1 + 2", write "+ 1 2"). trying work out why 1+
function exists.
what reason prefer (+ 1 2)
or (1+ 2)
?
is syntactic sugar? code optimisation? readability?
perhaps there more complex function call examples show why 1+
function exists. insight appreciated.
do remember part of common lisp standardizing many implementations had. so, if many implementations had 1+ function, have been enough include it. rainer's answer quotes cltl2 on which implementations had (maclisp , lisp machine lisp). why implementations have had in first place? it's useful in loops, e.g.,
(do ((x 0 (1+ x))) ((= x 10)) ; ... )
that's place (+ x 1) have been fine, too. there lots of cases it's handy call kind of function indirectly, , it's easier (mapcar '1+ …) (mapcar (lambda (x) (+ 1 x)) …).
but that's it. adding 1 (or subtracting one; there's 1- well) such common operation it's handy have function it. if there's hardware support, might implementation can optimize, too. (although the documentation note "implementors encouraged make performance of [(1+ number) , (+ 1 number)] same.") since these functions available , used, they're way indicate intent. e.g., make typo , write (+ 1 x) when meant write (+ 2 x), it's less wanted add 2 if wrote (1+ x). these functions idiomatic in common lisp (e.g., see how increment or decrement number in common lisp?). emacs lisp includes 1+ , 1-.
the language wouldn't suffer if weren't there, reimplemented many times many different people. instance, racket implements add1 , sub1. (also, see add1 function scheme r5rs.)
Comments
Post a Comment