Building a List recursively from Facts

时间:2017-04-24 17:30:02

标签: prolog

I'm having trouble building a list from Facts in prolog. I have some Facts of "means" where one letter maps to another like a cypher. I would like to write a "decoder" that takes a list of the coded message character and return the list of the mapped to values.

 means(a,z). 
 means(z,a).
 means(b,y). 
 means(y,b).
 means(c,x). 
 means(x,c).
 means(d,w). 
 means(w,d).
 means(e,v). 
 means(v,e).
 means(f,u). 
 means(u,f).
 means(g,t). 
 means(t,g).
 means(h,s). 
 means(s,h).
 means(i,r). 
 means(r,i).
 means(j,q). 
 means(q,j).
 means(k,p). 
 means(p,k).
 means(l,o). 
 means(o,l).
 means(m,n). 
 means(n,m).

Here are some examples of correct behaviour for decode procedure:

 ?- decode([r,o,l,e,v,k,i,l,o,l,t],X).
 X = [i, l, o, v, e, p, r, o, l, o, g] ;
 No
 ?- decode(Y,[i,l,o,v,e,p,r,o,l,o,g]).
 Y = [r, o, l, e, v, k, i, l, o, l, t] ;
 No

this is my version of "decode" I'm new to prolog and I'm sure I'm missing something major. I know you cannot modify a list like you can in other languages add(X,L,[X|L]).

 decode([],L).

 decode([A|B],L) :-
  means(A,X),
  add(X,L,C),
  decode(B,C).

I know in "add" I'm adding to the front of the list and not the end, but I don't know how to do this in prolog. Any help would be greatly appreciated.

Thank you

0 个答案:

没有答案