1

What I have to do:

Define a SCHEME procedure, named (combine f Ha Hb), which accepts three arguments, f, a firstorder relation which is used to order the elements in the heap, and two heap structures, Ha and Hb, which have been constructed using the same first-order relation.

Example of a test case:

( define Ha ( heap-insert-list > ( list 9 5 7 3) ( list )))
( define Hb ( heap-insert-list > ( list 2 8 4 6) ( list )))
( combine > Ha Hb )
(9 (7 () (5 () (3 () ()))) (8 (4 () ()) (6 () (2 () ()))))

My code:

(define (create-heap v H1 H2)
(list v H1 H2))
(define (h-min H) (car H))
(define (left heap)
  (cadr heap))
(define (right heap)
  (caddr heap))

(define (combine f Ha Hb)
  (cond ((null? Ha) Hb)
    ((null? Hb) Ha)
    ((< (h-min Ha) (h-min Hb))
     (create-heap (h-min Ha)
                  Hb
                  (combine (left Ha) (right Ha))))
    (else
     (create-heap (h-min Hb)
                  Ha
                  (combine (left Hb) (right Hb)) Ha))))

My code is doing something right as I am getting 50% on my test cases, but it is not completely passing them.

Óscar López
  • 232,561
  • 37
  • 312
  • 386

1 Answers1

1

Well, for starters you're not using the f procedure! This:

((< (h-min Ha) (h-min Hb))

... Most likely should look like this:

((f (h-min Ha) (h-min Hb))

Also, you forgot to pass the f parameter when calling the recursion:

(combine f (left Ha) (right Ha))
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • That makes sense. However, when trying to implement it all my test cases failed. Im more-so stuck on what three parameters my combine function should be taking instead of the two in my code (left Ha, right Ha). – carbonblast Mar 31 '20 at 19:47
  • You also forgot to pass the `f` parameter when calling the recursion, the code in the question was not compiling in the first place. – Óscar López Mar 31 '20 at 19:51