Page 1 of 1

Ordering a list depending on the order of elements in anothe

Posted: Thu Apr 26, 2012 5:03 am
by halastoi
I have a list

Code: Select all

(SetQ L '(1 j 3 k 4 h 5 n 6 w))
I have to do a function Order that has a list with 'n' atoms at entry, it must check if each atom of that list is contained in list L and order them according to the order specified in list L, if the atom is not part of the list L then the result will be displayed

Code: Select all

(Defun Order lst)

(SetQ L2'(w o 5 j 3))
I want to verify this:

Code: Select all

(Order L2)
result should return:

Code: Select all

(J 3 5 W)

Re: Ordering a list depending on the order of elements in an

Posted: Thu Apr 26, 2012 9:04 am
by edgar-rft
Sorry, but this forum is not called "we make your homework".

Re: Ordering a list depending on the order of elements in an

Posted: Thu Apr 26, 2012 9:23 am
by nuntius
I moved the topic to the homework forum. The source language is CL.

Re: Ordering a list depending on the order of elements in an

Posted: Fri Apr 27, 2012 9:00 pm
by jecxjo
So this problem is designed to step through one list and at each atom check if it exists in the other.

This problem is asking you to make two recursive tasks, one based on L and one on L2. I suggest you start with L as it defines your order.

L's function:
If null, return nil
else If L2 contains car L return car L
else call L's function with cdr L.

L2's funciton:
If L2 null, return nil
else if car L equals car L2 return car L2
else call L2's function with cdr L2

Put those two together and there you go.