MC logo

CS 231 Assignment 2


CS 231 Programming Assignments

CSC 231

Assignment 2
Due: October 2, Midnight
45 Pts.

Create a function my_merge to merge two lists of numbers. (The name merge is already a standard lisp function). My_merge should take two arguments. It assumes that each argument is a list of numbers sorted in ascending order. There may be repeated members, and the lists need not be the same length. The function returns a sorted list containing all the items from either input list. For instance:

[7]> (my_merge '(4 9 18 32 32 45 59) '(1 2 15 15 16 18 21 33))
(1 2 4 9 15 15 16 18 18 21 32 32 33 45 59)
[8]> (my_merge '(4 5 67) '(11))
(4 5 11 67)
[9]> (my_merge '() '(4 9 11 8))
(4 9 11 8)
[10]> (my_merge () ())
NIL
[11]> (my_merge '(34) '(10))
(10 34)

Place the definition of your function in a file which can be loaded by clisp. Please observe the following:

Discussion

You will probably want to use a cond to consider several cases. You will have a basis case when one of the argument lists is empty.

When both lists are non-empty, consider that the merge of two sorted lists can be constructed as follows:

  1. Remove the head of the list whose head is the smallest.

  2. Use recursion to merge the tail of this list and the whole of the other list.

  3. Add the item removed in the first step to the front of the list created in the second. This is the final result.

Submission

Submit your program using the form here.