Search code examples
prologprolog-findall

Findall inside findall


it is possible make something like this?

--- knowledge base ---

linha( 5,[bobigny_pablo_picasso,bobigny_pantin_raymon_queneau,eglise_de_pantin,hoche,
          porte_de_pantin,ourcq,laumiere,jaures,stalingrad,gare_du_nord,gare_de_l_est,
          jacques_bonsergent,republique,oberkampf,richard_lenoir,breguet_sabin,bastille,
          quai_de_la_rapee, gare_dausterlitz,saint_marcel,campo_formio,place_ditalie],
         [10,2]).

route(71,[louis_blanc,jaures,bolivar,buttes_chaumont,botzaris,place_des_fetes,
          pre_saint_gervais],
         [10,2]).

--- knowledge base end ---

sameRoute(EI,NF):-
    findall(Y,findall(Z,member(EI,route(Y,Z,_)),NumRout),NumRouteF),
    append(NumRout,NumRouteF,NF).

EI is a station, and NF its supposed to return the number in the route the first parameter of the predicate route.

What am I doing wrong?


Solution

  • From the comments it seems you need all the routes that pass through a given station S.

    findall(R, (route(R, Stations, _), member(S, Stations)), Routes).
    

    The goal is a conjunction of two conditions: that R is the number of some route that goes through the list of stations Stations and that the given station S is a member of that list.

    ?- S = louis_blanc, findall(R, (route(R, Stations, _), member(S, Stations)), Routes).
    S = louis_blanc,
    Routes = [7, 71].