In: Computer Science
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects.
This problem needs to use DrRacket software. Racket Language.
Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarification. (indices '(a b c a e f a) 'a') ---> (0 3 6)
(indices '(a (a b) b c d) 'a) ---> (0)
(indices '(a b c a e f a) 'z) ---> ()
(indices '(() (a b) a b ()) '()) ---> (0 4)
Code:
define i 0) ;;intialized i =0 to keep track of indices
(define (indices L1 X) ;;MAIN Function
(positions L1 X i)) ;;calls sub function which returns list of all
indices of an element
(define (positions L1 X i)
(cond ((null? L1) '()) ;;if element is not found returns '()
((equal? (car L1) X) (cons i (positions (cdr L1) X (+ i 1))))
;;element is found at index i and continue
(else (positions (cdr L1) X (+ i 1))))) ;;element not found, continue in rest of the list
Snapshots of Code and Output: