Write a Scheme function (remove x lis) that remove the first occurance of x in a
ID: 3816514 • Letter: W
Question
Write a Scheme function (remove x lis) that remove the first occurance of x in a simple list "lis". For example, after you have defined your remove function and run (remove 2 '(3 2 4)) will return (3 4) (remove 2 '(2)) will return empty (remove 1 '(3 2 4)) will return (3 2 4) Submit .scm file that implements the function, and submit hardcopy that contain the function definition and test results such as above. You can get the hardcopy by printing the screen, or (better) copying and pasting to a WORD file that looks better. You don't have to email the WORD file (just print and submit). The file starts with comment like: ;NAME: YOUR_NAME ;FILENAME: USER_NAME.scm ;DATE: Type your code in the upper text area like: (define (remove x lis) .......) and then run in the lower text area like: (remove 2 '(2 4))
Explanation / Answer
(define (remove x lst)
(cond ((null? lst) '())
((equal? (car lst) x) (cdr lst))
(else (cons (car lst)
(remove x (cdr lst))))))
----------------------------------------------------
for example:
(remove '(1 2) '((1 2) (3 4) (1 2)))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.