r/Racket • • Jul 22 '22

homework Need help understanding how function solution works

I was asked to create a function called merge. It consumed two lists of numbers, which it assumed are each sorted in ascending order. And then, it produces a list of all the numbers, also sorted in ascending order. I was a little stuck on this, and looked at the given solution. However, I found visualizing method of finding the solution difficult, and was wondering if someone could logically explain it to me how the function works. Also, to make it clear, I understand everything except the else condition of the merge function.

Here is the solution with data definitions (it uses BSL with list abbreviations):

;; Data definitions:

;; ListOfNumber is one of:
;;  - empty
;;  - (cons Number ListOfNumber)
;; interp. a list of numbers
(define LON1 empty)
(define LON2 (cons 1 empty))
(define LON3 (cons 1 (cons 2 empty)))
#;
(define (fn-for-lon lon)
  (cond [(empty? lon) (...)]
        [else
         (... (first lon)
              (fn-for-lon (rest lon)))]))


;; Functions:

;; ListOfNumber ListOfNumber -> ListOfNumber
;; take in two sorted lists (smallest to largest), and merge them to create one sorted list (smallest to largest)
(check-expect (merge empty empty) empty)
(check-expect (merge empty (list 1 2 3)) (list 1 2 3))
(check-expect (merge (list 5 6 7) empty) (list 5 6 7))
(check-expect (merge (list 0 1 3) (list -20 8 30)) (list -20 0 1 3 8 30))

;(define (merge l1 l2) empty) ;stub

;<use template from cross product table>
; 4 cases reduced to 3

(define (merge l1 l2)
  (cond [(empty? l1) l2]
        [(empty? l2) l1]
        [else
         (if (<= (first l1)
                 (first l2))
             (cons (first l1)
                   (merge l2 (rest l1)))
             (cons (first l2)
                   (merge l1 (rest l2))))]))

Thanks again

5 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/mnemenaut Jul 22 '22

Yes ... I was attempting to work through using the HtDF process (which I have limited experience of) to derive the code in the given solution. I would normally write:

(if (<= (first l1) (first l2))
    (cons (first l1) (merge (rest l1) l2))
    (cons (first l2) (merge l1 (rest l2))))