Prolog - Keep track of multiple sum counters

Schytheron

So I have a list that looks like this:

[ 
  ["p1", "p2", "100", "Storgatan"], 
  ["p1", "p3", "200", "Lillgatan"], 
  ["p2", "p4", "100", "Nygatan"], 
  ["p3", "p4", "50", "Kungsgatan"], 
  ["p4", "p5", "150", "Kungsgatan"]
]

The elements in each nested list represent (in order):
1st element = Start Point
2nd element = End Point
3rd element = Distance
4th element = Street Name.

I have to now write a predicate which figures out which street is the shortest and which street is the longest, along with their respective (summed up) distances.

For example the final output should look something like this:

Longest street: Kungsgatan, 200
Shortest street: Storgatan, 100

I don't really understand why the start and end points are relevant information here. My current idea is to collect all the unique street names, put them in a separate list along with a counter for each street that starts at zero and then use that list to accumulate all of the distances for each separate street.

Something like:

create_sum_list([
   ["p1", "p2", "100", "Storgatan"], 
   ["p1", "p3", "200", "Lillgatan"], 
   ["p2", "p4", "100", "Nygatan"], 
   ["p3", "p4", "50", "Kungsgatan"], 
   ["p4", "p5", "150", "Kungsgatan"]
], SL).

SL= [[Storgatan, 0], [Lillgatan, 0],
     [Nygatan, 0], [Kungsgatan ,0]]

accumulate(SL, List).

List=[[Storgatan, 100], [Lillgatan, 200], 
      [Nygatan, 100], [Kungsgatan ,200]]

This is probably a stupid idea and there is probably a way better way to solve this. I have thought of many different ideas where I either reach a dead end or they are way too complex for such a "simple" task.

I can achieve this easily through "normal" imperative programming but I am new to logical programming and Prolog. I have no idea how to achieve this.

Help?

Thanks!

User9213

If you already have a list and you want to group by street name and sum the lengths, you must decide how you do the grouping. One way is to use library(pairs):

streets_lengths(S, L) :-
    maplist(street_name_and_length, S, NL),
    keysort(NL, NL_sorted),
    group_pairs_by_key(NL_sorted, G),
    maplist(total_lengths, G, GT),
    transpose_pairs(GT, By_length), % sorts!
    group_pairs_by_key(By_length, L).

street_name_and_length([_, _, N, L], L_atom-N_number) :-
    number_string(N_number, N),
    atom_string(L_atom, L).

total_lengths(S-Ls, S-T) :-
    sum_list(Ls, T).

You can use it like this:

?- streets_lengths([
   ["p1", "p2", "100", "Storgatan"],
   ["p1", "p3", "200", "Lillgatan"],
   ["p2", "p4", "100", "Nygatan"],
   ["p3", "p4", "50", "Kungsgatan"],
   ["p4", "p5", "150", "Kungsgatan"]
], SL).
SL = [100-['Storgatan', 'Nygatan'], 200-['Lillgatan', 'Kungsgatan']].

Since there can be many streets with the same length, the results are returned grouped by length. You can get the "shortest" and "longest" by getting the first and last element of the list, like this:

L = [First|_], last(L, Last)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Best way to keep track of the sum of the field of multiple foreign keys [Django]

In R, check multiple values multiple times, sum if conditions are met, keep track of position, export data, and repeat

Keep track of multiple checkboxes react

How to keep track of multiple matches in drools

Keep track of list values in multiple loops

XQuery basics - how to keep track of multiple variables?

keep track of multiple terminals from shell script

How to keep track of multiple versions of assemblies?

How to sum multiple results from a predicate in prolog?

Keep track of variables within class when using method multiple times

what is the logic of using an array to keep track of multiple setTimeouts

Is there a good way to keep track of source code files duplicated in multiple projects?

Read multiple files but keep track of which file is which dataframe in R

How do I keep track of multiple objects in Javascript

How do I keep track of cards in hand with multiple lists in dictionary?

Multiple Counters in XSLT

Multiple counters with react hooks

(Python) multiple counters in a for loop?

CSS: How to have multiple counters in nested counters

Prolog CSPs: Less than constraint with instantiated counters

Keep track of progress of a `map`

Keep track of password in Ubuntu

keep track of presence

Keep track of synchronization changes

How do you keep track of multiple properties of a string without traversing it multiple times?

Keep counters up to date for a cached website

Updating multiple counters in one query

Creating a matrix of multiple counters in R

Create counters for multiple Owl Carousel