0

I am trying to make something like a list dictionary in Prolog. I want to transfer a list of numbers from German to English.

Input: trnslt([eins,drei,neun],X). Output: X = [one,three,nine] I've thought of an idea to make an if statement but then I thought this won't work. I would be thankful if anyone provided me with the idea and the code to fully understand the concept.

Edit: I have read the answers on this question and got the clauses, but actually did not understand anything actually, so can someone tell me how it works?

  • Consider [this](https://stackoverflow.com/a/6683502/772868). – false Feb 20 '21 at 11:32
  • Hello again! I understood everything, but only one thing couldn't understand. ```listtrans([X|Xs],[Y|Ys]) :- trans(X,Y), listtrans(Xs,Ys).``` How does Prolog understand this rule, or in other meaning how can I understand it? – Omar Oshiba Feb 20 '21 at 13:45

1 Answers1

1

1- Define the facts.

2- translate predicate checks each German number in the list [H|T], then searches the facts number, and then stores the English number in the [X|List].

CODE:

%Facts
number(eins, one).
number(zwei, two).
number(drei, three).
number(vier, four).
number(fünf, five).
number(sechs, six).
number(sieben, seven).
number(acht, eight).
number(neun, nine).
number(zehn, ten).

translate([],[]).
translate([H|T],[X|List]):-
    number(H,X),
    translate(T,List).

Examples:

?- translate([eins,drei,neun],X).
X = [one, three, nine]

?- translate([drei],X).
X = [three]

?- translate([drei,zehn,acht,sechs],X).
X = [three, ten, eight, six]
Reema Q Khan
  • 878
  • 1
  • 7
  • 20