Saturday, December 19, 2009

Order a list as per list using scheme

The below program takes in a list of arguments and orders them as per the second list.

(define (nth lst1 lst2 n)
(if (= n (car lst2)) (car lst1) (nth (cdr lst1) (cdr lst2) n)))

(define (order lst1 lst2)
(define (order-help n)
(if (> n (length lst1)) '() (cons (nth lst1 lst2 n) (order-help (+ n 1)))))
(order-help 1))

(order '(a b c d e) '(1 2 3 4 5))
; Value 1: (a b c d e)
(order '(a b c d e) '(4 1 2 3 5))
; Value 2: (b c d a e)
(order '(a b c d e) '(5 4 3 2 1))
; Value 3: (e d c b a)
(order '(a b c d e) '(1 5 2 4 3))
; Value 4: (a c e d b)

1 comment:

Anonymous said...

Nice fill someone in on and this enter helped me alot in my college assignement. Say thank you you seeking your information.