1. Write a Scheme function to implement the following list manipulation operatio
ID: 3630720 • Letter: 1
Question
1. Write a Scheme function to implement the following list manipulation operationsf. Merge two (ascending) ordered lists of integers into a single (ascending) ordered list. Call it merge. For example:
(merge ‘(1, 2, 12) ‘(2, 3, 4, 5, 8)).
=> (1 2 3 4 5 8 12)
Make sure your solution handles duplicates appearing within the lists:
(merge(‘(1, 1, 12) ‘(2, 3, 4, 5, 8))
-> (1 1 2 3 4 5 8 12)
Explanation / Answer
Defining two lists (define l1(list 1, 1, 12)) (define l2(list 2, 3, 4, 5, 8)) keywords: car: returns first element in list cdr: returns a list except first element Function call (define (Merge l1 l2) (cond ((null? l1) l2) ((null? l2) l1) ((< (car l1) (car l2)) // is fist element of firs list less than (cons (car l1) (Merge (cdr l1) l2))) ((>= (car l1) (car l2)) // is fist element of second list greater than (cons (car l2) (Merge l1 (cdr l2)))) ))Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.