Search code examples
prolog

find minimum of list inside list on the basis of one value


I have this list in prolog

[[[dublin, london], 1000], [[dublin, moscow, london], 5000]]

And I want to calculate the minimum of the list such that answer should be

[[dublin, london], 1000]

This question has some similar problem minimum in list of lists in prolog

But that doesn't seems to working I am getting output as false. Please help


Solution

  • Try library(aggregate):

    ?- aggregate_all(min(X,Y), 
           member([Y,X], [[[dublin, london], 1000], 
                          [[dublin, moscow, london], 5000]]), 
           R).
    R = min(1000, [dublin, london]).
    

    See also here:

    Aggregation operators on backtrackable predicates
    https://www.swi-prolog.org/pldoc/man?section=aggregate