user3352349
user3352349

Reputation: 177

Merge sort output - scheme

I have a merge sort function that takes a predicate and a list of real values, it then sorts them according to the predicate. e.g.

(merge-sort > '(1 7 4 6)) 
    ---> (7 6 4 1)

My question is given the association lists:

(define *Mark*
    '( (age . 20)
       (gender . male))

(define *Judith*
    '( (age . 30)
       (gender . female))

(define *Elliot*
    '( (age . 40)
       (gender . male))

which have been used to create an unordered list (Mark Elliot Judith), how, using their ages in the merge sort

(merge-sort > '(20 40 30)) ---> (40 30 20)

can I output the ordered list

(Elliot Judith Mark)

Upvotes: 0

Views: 235

Answers (1)

C. K. Young
C. K. Young

Reputation: 223043

(merge-sort (lambda (a b)
              (define (get-age x)
                (cdr (assq 'age x)))
              (> (get-age a) (get-age b)))
            (list *Mark* *Judith* *Elliot*))

Upvotes: 2

Related Questions