Write a scheme function findloc which takes two parameters a

Write a scheme function find-loc 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

Solution

(define (find-loc L e)
(if (not (list? L)); check if the list is an atom
-1
(let ((len1 (len L)))
(cond ((null? L) 1); if the list (and it is a list so it must be empty) is null, we return 1 (1 longer than the list)
((eq? (car L) e) 0); if the element is the head return 0
((null? (cdr L) ) len1); if cdr is null, then return index of one past the last index (aka length)
(else (+ (find-loc (cdr L) e) 1)))))); otherwise recursively find it and return the location.

Write a scheme function find-loc which takes two parameters, a list lst and an atom atm, and returns the index of the first location where atm occurs in the lis

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site