Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**LANGUAGE IS SCHEME*** 5. Define a Scheme function, (merge 11 12), which takes

ID: 3598178 • Letter: #

Question

**LANGUAGE IS SCHEME***

5. Define a Scheme function, (merge 11 12), which takes two lists e1 and l2 as arguments. As- suming that each of l1 and l2 are sorted lists of integers (in increasing order, say), merge must return the sorted list containing all elements of l1 and l2 To carry out the merge, observe that since l and l2 are already sorted, it is easy to find the smallest element among all those in E and (2: it is simply the smaller of the first elements of (1 and l2. Removing this smallest element from whichever of the two lists it came from, we can recurse on the resulting two lists (which are still sorted), and place this smallest element at the beginning of the result.

Explanation / Answer

//This is our function which would merge the already sorted list. (define (merge l1 l2) //We would check whether the first list is null, if yes return second list (cond ((null? l1) l2) //We would check whether the second list is null, if yes return second list ((null? l2) l1) //We would compare the first elements of both list by getting them using car //If element from l1 is smaller, we would create a list using this element (from l1) and the output of merging the remaining list items of l1 and l2. ((< (car l1) (car l2)) (cons (car l1) (merge (cdr l1) l2))) //Similarly,If element from l2 is smaller, we would create a list using this element (from l2) and the output of merging the remaining list items of l1 and l2. (else (cons (car l2) (merge l1 (cdr l2))))))