We Recommend

Learning Perl Learning Perl
In this smooth, carefully paced course, a leading Perl trainer teaches you to program in the language that threatens to make C, sed, awk, and the Unix shell obsolete for many tasks. This book is the "official" guide for both formal (classroom) and informal learning. It is fully accessible to the novice programmer.


Ballyhoo


Posted By

gleather on 01/14/08


Tagged

scheme list-index eopl


Versions (?)


list-index (Scheme)


Published in: Lisp 


This one drove me crazy because of the requirement to return -1 if s not in los.

(list-index s los) returns the zero-based index of the first
occurence of s in los, or -1 if there is no occurences of s in los.


  1. (define list-index
  2. (lambda (s los)
  3. (if (null? los)
  4. -1
  5. (if (eq? (car los) s)
  6. 0
  7. (if (= (list-index s (cdr los)) -1)
  8. -1
  9. (+ 1 (list-index s (cdr los))))))))

Report this snippet 

You need to login to post a comment.