12

This is trivial implement of course, but I feel there is certainly something built in to Racket that does this. Am I correct in that intuition, and if so, what is the function?

3 Answers 3

19

Strangely, there isn't a built-in procedure in Racket for finding the 0-based index of an element in a list (the opposite procedure does exist, it's called list-ref). However, it's not hard to implement efficiently:

(define (index-of lst ele)
  (let loop ((lst lst)
             (idx 0))
    (cond ((empty? lst) #f)
          ((equal? (first lst) ele) idx)
          (else (loop (rest lst) (add1 idx))))))

But there is a similar procedure in srfi/1, it's called list-index and you can get the desired effect by passing the right parameters:

(require srfi/1)

(list-index (curry equal? 3) '(1 2 3 4 5))
=> 2

(list-index (curry equal? 6) '(1 2 3 4 5))
=> #f

UPDATE

As of Racket 6.7, index-of is now part of the standard library. Enjoy!

17
  • 2
    How bizarre. Would the racket-dev mailing list be the appropriate outlet for recommending this feature to be added to the language?
    – Alex V
    Apr 8, 2013 at 4:15
  • 1
    I guess ... but it's not a big deal given that it's so simple to implement. Apr 8, 2013 at 4:16
  • 2
    @Maxwell I had forgotten about list-index. See my updated answer. Apr 8, 2013 at 4:29
  • 1
    Thanks Oscar, I am going to use that, you are amazing.
    – Alex V
    Apr 8, 2013 at 4:43
  • 4
    As of Racket 6.7 index-of is now part of the standard library! This might be worthy of an edit (although this answer works perfectly fine).
    – djfdev
    Sep 28, 2017 at 23:13
8

Here's a very simple implementation:

(define (index-of l x)
  (for/or ([y l] [i (in-naturals)] #:when (equal? x y)) i))

And yes, something like this should be added to the standard library, but it's just a little tricky to do so nobody got there yet.

Note, however, that it's a feature that is very rarely useful -- since lists are usually taken as a sequence that is deconstructed using only the first/rest idiom rather than directly accessing elements. More than that, if you have a use for it and you're a newbie, then my first guess will be that you're misusing lists. Given that, the addition of such a function is likely to trip such newbies by making it more accessible. (But it will still be added, eventually.)

0
3

One can also use a built-in function 'member' which gives a sublist starting with the required item or #f if item does not exist in the list. Following compares the lengths of original list and the sublist returned by member:

(define (indexof n l)
  (define sl (member n l))
  (if sl 
      (- (length l)
         (length sl))
      #f))

For many situations, one may want indexes of all occurrences of item in the list. One can get a list of all indexes as follows:

(define (indexes_of1 x l)
  (let loop ((l l)
             (ol '())
             (idx 0))
    (cond
      [(empty? l) (reverse ol)]
      [(equal? (first l) x)
       (loop (rest l)
             (cons idx ol)
             (add1 idx))]
      [else
       (loop (rest l)
             ol
             (add1 idx))])))

For/list can also be used for this:

(define (indexes_of2 x l)
  (for/list ((i l)
             (n (in-naturals))
             #:when (equal? i x))
    n))

Testing:

(indexes_of1 'a '(a b c a d e a f g))
(indexes_of2 'a '(a b c a d e a f g))

Output:

'(0 3 6)
'(0 3 6)
1
  • 2
    Please please don't do the -1 thing to signal that there is no index. It's much clearer and easier if you return #false in those cases, so that's the idiomatic way to do it Dec 8, 2016 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.