Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • About Bonfire
Simon Brooke
Simon Brooke
@simon_brooke@mastodon.scot  ·  activity timestamp 4 days ago

SBCL #CommonLisp
(nth 2 '(1 2 3 4 5 6))
3

#Clojure
(nth '(1 2 3 4 5 6) 2)
3

#Scheme
(list-ref '(1 2 3 4 5 6) 2)
;Value: 3

So, they differ on the name of the function and the order of the arguments, but they all agree on zero-indexing the first element of a list.

This seems perverse to me. Surely the index of the first element should be 1, the second, 2, and so on?

Argue against me -- and please give an argument which is not just **TRADITION!**

#Lisp

  • Copy link
  • Flag this post
  • Block
Michael Grinder
Michael Grinder
@mgrinder@mastodon.social  ·  activity timestamp 4 days ago

@simon_brooke The way to think about zero-indexing is that it measures the distance from the beginning of the list. Since Lisp lists are linked lists, it is a reminder of how many additional links you need to traverse to get the item you want and that nth might not be the most efficient way to do things. (I don't prefer zero-indexing, but that's the best argument for it that comes to mind.) 🙂

  • Copy link
  • Flag this comment
  • Block
Dave
Dave
@daveliepmann@social.tchncs.de  ·  activity timestamp 4 days ago

@simon_brooke for Clojure at least it was a reflection of the host (Java)

for the common-sense "first" element there's...`first`

  • Copy link
  • Flag this comment
  • Block
Simon Brooke
Simon Brooke
@simon_brooke@mastodon.scot  ·  activity timestamp 4 days ago

@daveliepmann H'mmm... I just pulled the Lisp 1.5 Programmer's Manual off the shelf to check what it did, and... drumroll... it didn't.

There is no NTH function. So we cannot appeal to the wisdom of the ancients.

  • Copy link
  • Flag this comment
  • Block
Svante
Svante
@Ardubal@mastodon.xyz  ·  activity timestamp 4 days ago

@simon_brooke https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html

E.W. Dijkstra Archive: Why numbering should start at zero (EWD 831)

  • Copy link
  • Flag this comment
  • Block
Daniel Kochmański
Daniel Kochmański
@jackdaniel@functional.cafe  ·  activity timestamp 4 days ago

@simon_brooke

https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html

Mathematically it makes most sense, especially when you deal with index arithmetics. And it us proper from math point of view.

E.W. Dijkstra Archive: Why numbering should start at zero (EWD 831)

  • Copy link
  • Flag this comment
  • Block
João
João
@jmmonteiro@mastodon.social  ·  activity timestamp 4 days ago

Lisp semantics of nth

@simon_brooke
It is tradition / convention.
I guess you could frame it as a shared language in computer science, and a way to reduce cognitive load if you have to write code in multiple languages.

I personally find it more likely to write an off-by-one bug in the rare occasions when I have to switch to a language with 1-indexing.

  • Copy link
  • Flag this comment
  • Block
Paul SomeoneElse
Paul SomeoneElse
@pkw@snac.d34d.net  ·  activity timestamp 4 days ago

To me part of this is the idea mostly from C that
a variable is an atom or a sequence depending
on context. Using 0 as the first is nice because
the first one is special. It's the given one, if
it isn't there it is NULL.

Maybe this makes less sense in lisp
where an atom and a list are distinct.

  • Copy link
  • Flag this comment
  • Block
mousebot
mousebot
@mousebot@todon.nl  ·  activity timestamp 4 days ago

@simon_brooke i won't argue with you, but a side question: do you know where zero-indexing is from or in what contexts it makes (lots) more sense? i agree that here it seems kinda perverse, almost as if the first element wasn't really there.

  • Copy link
  • Flag this comment
  • Block
Tor Lillqvist
Tor Lillqvist
@tml@mementomori.social  ·  activity timestamp 4 days ago

@mousebot @simon_brooke In C, p[n] means exactly the same as *(p+n). Thus what looks like indexing an array with index zero must mean the same as looking at what a pointer points to with zero offset.

  • Copy link
  • Flag this comment
  • Block
Simon Brooke
Simon Brooke
@simon_brooke@mastodon.scot  ·  activity timestamp 4 days ago

@tml @mousebot I know. But C is a very low level language; Lisp is not.

  • Copy link
  • Flag this comment
  • Block
Tor Lillqvist
Tor Lillqvist
@tml@mementomori.social  ·  activity timestamp 4 days ago

@simon_brooke @mousebot Yes, and I was replying to mousebot who asked where zero-indexing comes from. I think older programming languages like FORTRAN used indexing starting with one, and C is the best-known slightly more recent one that uses zero-indexing.

  • Copy link
  • Flag this comment
  • Block
Simon Brooke
Simon Brooke
@simon_brooke@mastodon.scot  ·  activity timestamp 4 days ago

@tml @mousebot H'mmm... I'm still thinking about it. I *think* I'm going to go with 1 = 1, although I do understand the possible merits in deciding that 1 = 0.

  • Copy link
  • Flag this comment
  • Block
Simon Brooke
Simon Brooke
@simon_brooke@mastodon.scot  ·  activity timestamp 4 days ago

@mousebot I think this is just computer science history. In languages like C (and before that in assemblers), the first character in a string is at index 0 because it is the byte at the base address of the string, and the second character is at index 1 because it's addressed by adding 1 to the base index.

But in #Lisp we don't have any of this nonsense, so why should the first item on a list not have the common sense index of one?

  • Copy link
  • Flag this comment
  • Block

BT Free Social

BT Free is a non-profit organization founded by @ozoned@btfree.social . It's goal is for digital privacy rights, advocacy and consulting. This goal will be attained by hosting open platforms to allow others to seamlessly join the Fediverse on moderated instances or by helping others join the Fediverse.

BT Free Social: About · Code of conduct · Privacy ·
Bonfire social · 1.0.2-alpha.34 no JS en
Automatic federation enabled
Log in
Instance logo
  • Explore
  • About
  • Code of Conduct