0

How can I sort and merge two lists based on the operator in Racket-Plait? > ascending, < descending order. This is what I have so far, but I do not have any idea what to do next.

(define (merge [op : (Number Number -> Boolean)]
               [int-list1 : (Listof Number)]
               [int-list2 : (Listof Number)]) : (Listof Number)
  (cond
    [(equal? op <) "something"]
    [(equal? op >) "do something"])) 

(test (merge < '(1 4 6) '(2 5 8))
      '(1 2 4 5 6 8))

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • If this is homework, could you add the exact instructions? Which parts of your code are included in the instructions (so the solution should include them) and which are yours? – Martin Půda Jan 28 '23 at 18:33

1 Answers1

1

I think you're assuming that this is more complicated than it is.

You're not supposed to do different things depending on the value of op - op is the ordering predicate you should use for merging and the predicate that the input lists are ordered by.
(In contrast to many other languages, symbols like < or > are not "special" in any way, they are identifiers just like less, or greater, or op)

Here is a skeleton special case to get you started; the interesting bits are left as an exercise:

(define (merge [l1 : (Listof Number)]
               [l2 : (Listof Number)]) : (Listof Number)
  (cond [(empty? l1) l2]  ;; If either list is empty,
        [(empty? l2) l1]  ;; the result is the other list.
        [(< (first l1) (first l2)) ... fill this in ...]
        [else ... fill this in ...]))

then, notice that this is equivalent to the code above:

(define op <)

(define (merge [l1 : (Listof Number)]
               [l2 : (Listof Number)]) : (Listof Number)
  (cond [(empty? l1) l2]  ;; If either list is empty,
        [(empty? l2) l1]  ;; the result is the other list.
        [(op (first l1) (first l2)) ... fill this in ...]
        [else ... fill this in ...]))

and then you pass op as a parameter instead, and you're done.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82