0

If I have a list like this

(1 2 3) And I want to find the index of 2,I used the function

 (position '3 '(1 2 3))

when compiling this error was occurred

. . position: undefined; cannot reference undefined identifier

Ala Aga
  • 399
  • 2
  • 4
  • 12

1 Answers1

2

If I understand you correctly, you simply want to find the index of an element in a list. I haven't found a build-in procedure for this, but you can do this easily yourself:

(define (position elt lst)
  (let loop ((lst lst) (i 0))
    (cond
      ((null? lst) #f)
      ((eq? elt (car lst)) i)
      (else (loop (cdr lst) (+ 1 i))))))

then

(display (position '3 '(1 2 3)))
=> 2
uselpa
  • 18,732
  • 2
  • 34
  • 52
  • @Alaa'Agha The accepted answer to [How do I find the index of an element in a list in Racket?](http://stackoverflow.com/q/15871042/1281433) mentions that there's no such built in function in Racket, but that there are some similar functions in some of the SRFIs that are very close. – Joshua Taylor Dec 17 '13 at 01:28