0

I'm new to Scheme, and I don't know where to start with this. I want to write a Scheme function, which takes two parameters, a list lst and an atom atm, and returns the index of the first location where atm occurs in the list. The location index is 1-relative. If atm does not occur in the list, the function returns n + 1, where n is the length of the list.

How can I do this?

Alexis King
  • 43,109
  • 15
  • 131
  • 205
joe
  • 33
  • 5

2 Answers2

1

Here's a skeletal solution. Replace all the <???> with appropriate items.

(define (index-of lst x)
  (cond ((null? lst) <???>)
        ((eq? <???> x) <???>)
        (else (+ (index-of <???> x) 1))))
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
0

You make an auxiliary procedure or named let with your argument list and a index initialized to 1.

If the list is empty or current car is the element you are looking for you return index or else you recurse with the cdr and increase index by 1.

Sylwester
  • 47,942
  • 4
  • 47
  • 79