Omar Oshiba
Omar Oshiba

Reputation: 89

How to translate a list into another in Prolog

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?

Upvotes: 0

Views: 205

Answers (1)

Reema Q Khan
Reema Q Khan

Reputation: 878

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]

Upvotes: 1

Related Questions