In: Computer Science
Please use the Scheme programming language with Dr. Racket to solve a and b below. (Use Scheme and not Python)
Write before–in–list?, which takes a list and two elements of the list. It should return #t if the second argument appears in the list argument before the third argument:
> (before–in–list? '(back in the ussr) 'in 'ussr) #T
> (before–in–list? '(back in the ussr) 'the 'back) #F
The procedure should also return #f if either of the supposed elements doesn't appear at all.
Code:
(define (before-in-last? list x y)
(cond
((null? list) #f) ;; if list empty returns false
((equal? (car list) x) (member? y (cdr list))) ;;if second arg =
1st element of list, check for third arg in rest of the list
((equal? (car list) y) #f) ;;if third argument is 1st element of
the list return true as it has to appear after second
argument
(else (before-in-last? (cdr list) x y)))) ;;otherwise recursively
traverse rest of the list
(define (member? a list) ;;checks an element is member of list or
not
(cond ((null? list ) #f) ;;if list is empty returns false
((equal? (car list)a) #t) ;;if searching element is equal to first
member of list returns true
(else (member? a (cdr list))))) ;;otherwise check in remaining
list
Snapshot of Code and Output: