How to join 2 elements of a list in prolog

0

I have 2 lists.

strong_price ([100,110,95,90]). plato_fuerte ([filete_de_cerdo, fillet_of_fish, pollo_asado, meatloaf]).

What I want to do is a query that when you enter one of the dishes you also see its price

    
asked by 30.05.2017 в 04:39
source

1 answer

0

Just as you are storing the plates and their prices, you have to get the position of the plate in one of the lists and then get the price for the position in the other.

plato(X,P) :-
    plato_fuerte(Pls),
    precio_fuerte(Prs),
    nth0(I,Pls,X),
    nth0(I,Prs,P).

The nth0(?Index, ?List, ?Elem) predicate is true when Elem is the element in the Index position in the List list.

Now you can list all the dishes next to their price:

?- plato(X,Y).
X = filete_de_cerdo, Y = 100 ;
X = filete_de_pescado, Y = 110 ;
X = pollo_asado, Y = 95 ;
X = albondigas, Y = 90.

Ask about the price of a specific dish:

?- plato(filete_de_cerdo,X).
X = 100.

Or ask for the dishes with a certain price:

?- plato(X,90).
X = albondigas.
    
answered by 30.05.2017 / 07:04
source