Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Sorting a list of tupples - Maze exercise

Sorting a list of tupples - Maze exercise

Sorting a list of tupples - Maze exercise

hello, I'm having problems to sort a list consisting of tuples, imagine a maze with lines x columns, I get an initial position in the form of tuple: (top row, starting column) and also get a final position (end line, final column) and still get a list composed of elements that are tuples of the form: (direction, row, column), the direction can be, up down, the list is composed of the possible movements. and my predicate must return a list with the same elements that receives but ordered according to two conditions:
1 order by distance to the end position
2 in case of the distance to the end position equal to 2 or more elements of the list I get, I have to order those who have the same distance according to the largest distance to the starting position, that is, the farther from the end position has elements first.
Note: the maximum list I get with possible movements may have 4 elements

Here is what I tried so far:

CODE --> Prolog

distance ((L1, C1), (L2, C2), Dist): - Dist is abs (L1-L2) + abs (C1-C2)

ordena_poss (Poss, Poss_ord, Inicial_position, Final_position): -
length (Poss, Lenght_of_Poss)
((Length_of_Poss = 0, size = 1) -> Poss_ord = Poss, Poss Poss_ord =) 

% If lenght of Poss is 0 or 1 Poss_ordn = Poss (comment)

Poss - is the list you get with possible moves
eg [(c, 4, 4), (d, 5, 5)]

Inicial_position - is the place of departure of the maze. eg, (1, 6)
Final_position - is the place of arrival of the maze. eg (5, 6))

In the end result should be a Poss_ord list containing the same elements of Poss but ordered in the distance.

In this example when I call:

CODE --> Prolog

? - Ordena_poss ([(c, 4, 4), (d, 5, 5)], Poss_ord (1, 6), (5, 6)). 

should give the result:

CODE --> Prolog

Poss_ord = [(d, 5, 5), (c, 4, 4)] 

The code has now already works for when I get an empty or with only one element list.

If anyone has any idea how to solve this problem, I thank you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close