4 ms·
Common Lisp has functions first, second, last, etc. It has rest too. (first '(a b c d)) returns 'a (rest '(a b c d)) returns '(b c d) (third '(a
by todd8 6y ago
Common Lisp has functions first, second, last, etc. It has rest too.
(first '(a b c d)) returns 'a
(rest '(a b c d)) returns '(b c d)
(third '(a b c d)) returns 'c
(nth 0 '(a b c d)) returns 'a
(nth 1 '(a b c d)) returns 'b
(last '(a b c d) 2) returns '(c d)
I find these quite intuitive, and after a bit of use one gets accustomed to Lisp's three ancient functions: car, cdr, and cons. car is the same as first and cdr is the same as rest.
In comparison to handling lists in more modern languages, Lisp seems overly verbose and a bit cumbersome. To be fair, there has been over half a century of evolution in programming languages since Lisp was originally designed.
Early Lisp was an alternative to programming in Fortran II, Fortran IV, or Algol 60. These are very primitive languages with significant hurdles for programmers trying to do non-numeric computations (e.g. AI programs). Lisp's underlying memory model of garbage collected cons cells gave it so much flexibility compared to Fortran's fixed length arrays containing only numbers. Fortran had no dynamic lists, no dynamic vectors or slices, no dictionary/hashmap types, no records or structs, no variable length strings, no sets, no tuples. Assembly language often appeared as a reasonable alternative.
Here are a few Fortran if statements appearing in a popular programming book of the 60's (I still have it on my bookshelf):
IF (x - 2.1) 40, 40, 30
This if will always jump to either line labeled 40 or line labeled 30 of the program.
Here's another:
IF(I.GE.20.AND.I.LE.42.AND.J.GE.20.AND.J.LE.42) GO TO 702
Lisp's cond (the special form used like if) may seem odd now, but compared to the other languages of the time it was so much more expressive.
Because I'm an Emacs user, I use Lisp frequently just to keep my configuration tweaked the way I like it, but if I was creating an editor like Emacs today I think it would be better to use Python, Javascript, or Lua for the underlying programmable part of the editor.
Perhaps Haskell is heading for the same kind of niche that Common Lisp occupies today, remaining important for the ideas it explored and of historical interest but never becoming more popular than it is today.
- deleted 6y ago[deleted]
- pierrebai 6y agoCurious: why last takes the quantity second, unlike nth? That inconsistency would drive me mad.
- Jtsummers 6y agoThe default behavior of last is literally returning the last cons. So: (last '(1 2 3)) ;; => (3) The signature for last is: last list &optional n => tail If n is not provided, it defaults to 1 so the behavior and order of parameters is sensible in this context. http://www.lispworks.com/documentation/HyperSpec/Body/f_last.htm http://www.lispworks.com/documentation/HyperSpec/Body/f_last... It's also worth noting that along with nth which takes the index first, there's elt which takes a sequence as the first parameter and index as the second. aref is similar but restricted to arrays and permits multiple numbers for the subscripts since arrays can be multidimensional. char, which accesses characters in a string, takes the string first and index second as well. bit takes the bit array as the first parameter and the subscripts follow it.