toshiro92
toshiro92

Reputation: 1344

Count in prolog - Impossible to do

EDIT : More simple :

I changed my code to simplify. A predicate "nbarret" return the numbers of stations i want.

So there is my new code, but it doesn't change anything :

nb_stations([],0).
nb_stations([S,Li,Dir,SS],X):-nbarret(Li,S,SS,Dir,Y),X is X1 + Y.
nb_stations([S,Li,Dir,SS|Tr],X):-
     nbarret(Li,S,SS,Dir,Y),nb_stations([SS|Tr],X is X1 + Y).

In this case, i have an error :

ERROR: is/2: Arguments are not sufficiently instantiated
Exception: (8) (_G2031 is _G2270+1)is _G2711+5 ? creep
Exception: (7) nb_stations([charles_de_gaulle_etoile, m6, nation, bir_hakeim], _G2031 is _G2270+1) ? creep
Exception: (6) nb_stations([la_defense, rerA, vincennes, charles_de_gaulle_etoile, m6, nation, bir_hakeim], _G2031) ? creep

/-------------------------------------------------------------/

Old code (deprecated, i keep for comprehension) :

nb_stations([S,Li,Dir,SS|Tr],X):-num_stations(S,Li,Dir1,ND,_,_),Dir=Dir1,!,
num_stations(SS,Li,Dir1,NA,_,_),Dir=Dir1,!,
calculer(ND,NA,Y),X is X1 + Y,nb_stations([SS|Tr],X).

nb_stations([S,Li,Dir,SS|Tr],X):-num_stations(S,Li,_,_,Dir2,ND),!,
num_stations(SS,Li,_,_,Dir2,NA),!,
Dir=Dir2,!,calculer(ND,NA,Y),X is X1 + Y,nb_stations([SS|Tr],X).

calculer(ND,NA,X):-X is ND - NA.

More Details :

When you call nb_stations, you have to inform a path in a List, with the departure station, the lign of the transport, the direction, and then the station you will stop. If we have more next, it will be the correspondence. In this example : nb_stations([la_defense,rerA,vincennes,charles_de_gaulle_etoile,m6,nation,bir_hakeim],X).

You start in la_defense, you take the "rerA" transport, and you take "vincennes" for the direction. Then you stop in "charles_de_gaulle_etoile", and you take the m6 (metro), the direction is "nation", and you are arrived in "bir_hakeim". So my code count the number of stations i pass in this travel.

Upvotes: 0

Views: 171

Answers (2)

tempi
tempi

Reputation: 126

You need to change the order when X is assigned.

nb_stations([S,Li,Dir,SS|Tr],X):-
    num_stations(S,Li,Dir1,ND,_,_),
    Dir=Dir1,!,
    num_stations(SS,Li,Dir1,NA,_,_),
    Dir=Dir1,!,
    calculer(ND,NA,Y),
    nb_stations([SS|Tr],X1),
    X is X1 + Y.   % Change here

nb_stations([S,Li,Dir,SS|Tr],X):-
    num_stations(S,Li,_,_,Dir2,ND),!,
    num_stations(SS,Li,_,_,Dir2,NA),!,
    Dir=Dir2,!,
    calculer(ND,NA,Y),
    nb_stations([SS|Tr],X1),
    X is X1 + Y. % Change here

Upvotes: 1

joel76
joel76

Reputation: 5645

What do you want to write exactly ?

nb_stations([S,Li,Dir,SS|Tr],X):-
     num_stations(S,Li,_,_,Dir2,ND),!,
     num_stations(SS,Li,_,_,Dir2,NA),!,
     Dir=Dir2,!,
     calculer(ND,NA,Y),
     nb_stations([SS|Tr],X is X1+Y).

May be

nb_stations([S,Li,Dir,SS|Tr],X):-
     num_stations(S,Li,_,_,Dir2,ND),!,
     num_stations(SS,Li,_,_,Dir2,NA),!,
     Dir=Dir2,!,
     calculer(ND,NA,Y),

     X is X1 + Y,  <== here X1 is a free variable

     nb_stations([SS|Tr],X).

Upvotes: 0

Related Questions