2

I am trying to make a (somehow) delicate sorting function in Lisp. I know there is lambda operator that should make my work a lot easier, but I couldn't find anything helpful so I hope you can help me.

As an input I have a nested list like this one:

    ((o1 10 15 20) (o2 5 14 20) (o3 7 8 8))

The output should be a nested list like this one:

    ((o1 1 1 1) (o2 3 2 1) (o3 2 3 3))

To be more specific, the first element from o1 is compared to the first element from o2 and o3 and the return should be it's position (in the example above, 10 is greater than 5 and 7 so it will be on the first position in the resulting list) and so on.

The highest number will get the first position.

(it's like a score function. Some students make an application and their features numbers are compared. The one with highest number of features will get the first place, but when comparing the number of different technologies used, he may get the 2nd or 3rd place).

Thanks and I hope you can help me

3
  • 2
    Stackoverflow is not a service to turn problem specifications into code. What did you try so far? Commented May 29, 2013 at 9:50
  • I was asking because from what I know, there is a function that could do that, or at least help make it a lot easier. First, i defined a function that sorted a simple list, than i developed it to sort a nested list. So far, input: (setf o6 '((12 1 14)(5 12 13) (13 9 14))) Output: [18]> (sort o6 #'nestedList) ((5 12 13) (12 1 14) (13 9 14)). Now I am tring to make it sort it individual list... Commented May 29, 2013 at 10:06
  • So far i am sorting each nested list after a position using (sort nestedList #'< :key #'nth) With something like (list 2 (nth 2 (sort nestedList #'< :key #'second))) i can extract the list on a specific position, but i have no idea how to parametrizes the whole thing (lisp is complicated) Commented May 29, 2013 at 10:47

1 Answer 1

4

A little exploration is in order.

[3]> (setq a '((o1 10 15 20) (o2 5 14 20) (o3 7 8 8)))
((O1 10 15 20) (O2 5 14 20) (O3 7 8 8))

[4]> (setq b (apply #'mapcar #'list a))
((O1 O2 O3) (10 5 7) (15 14 8) (20 20 8))

[5]> (setq c (mapcar #'(lambda(x)(sort x #'>)) (cdr b)))
((10 7 5) (15 14 8) (20 20 8))

[6]> (mapcar #'(lambda(g)(cons (car g) (mapcar #'1+ 
        (mapcar #'position (cdr g) c)))) a)
((O1 1 1 1) (O2 3 2 1) (O3 2 3 3))

Now stir, form, and bake at 190 C until ready.

Sign up to request clarification or add additional context in comments.

1 Comment

Arigato ! It helped a lot !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.