On Apr 5, 10:08 pm, jon <
superuser...@googlemail.com> wrote:
> So is there scope for a new standard library function for this
> intuitive usage?
> (don't know what to call it, because to my mind 'nth' is the ideal
> name and the current 'nth' is more like an 'idx' (or 'ith') as is
> tries to look up values by their "index").
It always bothered me in Common Lisp that
(nth 1 list) == (second list)
but I guess it's traditional.
You could define something like this:
(defn ord
"Returns the 'nth' (one-based) item in the
sequence, nil if it does not exist. 'ord'
is short for 'ordinal'."
[n coll]
(first (nthrest coll (dec n))))
user=> (def xs [1 2 3])
#<Var: user/xs>
user=> (ord 1 xs)
1
user=> (ord 2 xs)
2
user=> (ord 3 xs)
3
user=> (ord 4 xs)
nil
user=> (ord 0 xs) ; nonsensical but works for now
1
I also noticed that this doesn't raise an exception:
user=> (nth nil 2)
nil
-Stuart