Question

I'm trying to write a rule to compare two atoms to see which one is better for example (memory_with_gb_2 is better than memory_with_gb_1) and what I've written and tried in SWI-prolog is the following:

better_attribute3_in(Attribute3_in,Attribute3):-
   atom_codes(Attribute3,List_Attribute3),
   startsWith(List_Attribute3,Attribute3_Start,Rest_Attribute3_List),
   atom_to_term(Rest_Attribute3_List,Attribute3_Number,_),
   number(Attribute3_Number),
   atom_codes(Attribute3_in,List_Attribute3_in),
   startsWith(List_Attribute3_in,Attribute3_in_Start,Rest_Attribute3_in_List),
   atom_to_term(Rest_Attribute3_in_List,Attribute3_in_Number,_),
   number(Attribute3_in_Number),
   Attribute3_in_Number>=Attribute3.

which is working perfectly in SWI-Prolog but when I try it in SICStus Prolog it just does not seem to work, is there anyway to implement the upper code in SICStus.

Était-ce utile?

La solution

I have trouble understanding what your code is intended to do and I do not think it works as it stands.

  1. The comparison can not be correct.
  2. atom_to_term/3 sounds like it takes an atom as first argument, your code looks like it passes a list.
  3. I do not know what startsWith/3 does, but I assume it is similar to append/3 with different argument order. In particular I assume it can succeed more than once.
  4. I suspect that better_attribute3_in(f22,f22), better_attribute3_in(f12,f22), and better_attribute3_in(f22,f12) are all true. Is this intentional? If so, what is better_attribute3_in/2 supposed to mean?

(To get something that mimics the original code you could probably replace startsWith(A,B,C) with append(B,C,A) and replace atom_to_term(A,B,C) with name(A,B)).

Autres conseils

I figured it it out thank you for your kind suggestion I changed my code to this:

better_attribute3_in(Attribute3_in,Attribute3):-
atom_codes(Attribute3,List_Attribute3),
startsWith(List_Attribute3,Attribute3_Start,
Rest_Attribute3_List),numeric(Rest_Attribute3_List),
number_codes(Attribute3_Number,Rest_Attribute3_List),
atom_codes(Attribute3_in,List_Attribute3_in),
startsWith(List_Attribute3_in,Attribute3_Start,Rest_Attribute3_in_List),
numeric(Rest_Attribute3_in_List),
number_codes(Attribute3_in_Number,Rest_Attribute3_in_List),
!,Attribute3_Number=<Attribute3_in_Number. 

using ascii codes to see if the contents of a list represent a number or no:

numeric(List):-subset(List,[48,49,50,51,52,53,54,55,56,57]).

and using startsWith to see if they start with the same string or not (for example I can compare two memories together but not a memory and a hard disk):

startsWith(OldString,[],OldString):- true.
startsWith([H|TOldString],[H|T],Rest):-
startsWith(TOldString,T,Rest).
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top