I want to transate no to Letters ,and letters to no,as a Example when germantoenglish([e,t,th],Y).Y shoul be like below

Y=[1,2,3]
but I get answer as false............

trans(e,1).
trans(t,2).
trans(th,3).
germantoenglish([S|H],[G|Ta]):-trans(H,Ta).
englishtogerman([J|Ta],[L|H]):-trans(H,Ta).

`

Recommended Answers

All 7 Replies

[S|H] is a pattern that says "this argument must be a list with at least one element. The first element shall be called S and H shall be a list containing all the other elements". You use [e, t, th] for this argument, so S becomes e and H becomes the list [t, th]. Then you call trans(H, Ta). So now prolog searches through the rules of trans to find one that matches the first argument [t, th]. However there is no rule for trans that accepts any kind of list, so no rule is found. Therefore you get false.

So there are basically two things wrong with your rule: 1. You call trans with a list as its first argument when it should be a symbol. 2. You never do anything with S.

trans should be called once for each symbol in the list with that symbol as its first argument. To accomplish that you'd either maplist or recursion. Note that when implementing recursion, you should add a rule for the case where the list you're trying to translate is empty.

commented: Very clear answer +14

I chanaged like this also but that also not working,Can you help me

trans(e,1).
trans(t,2).
trans(th,3).
germantoenglish([S|H],[G|Ta]):-trans(S,Ta),trans(H,Ta).
englishtogerman([J|Ta],[L|H]):-trans(H,Ta).
germantoenglish([S|H],[G|Ta]):-trans(S,Ta),trans(H,Ta).

Calling trans with S as the first argument is a step in the right direction, but by using Ta as the output, you're making Ta a symbol - it's supposed to be a list though. And you're still calling trans with H as the argument in the second call. As I said, you can't call trans with a list as the first argument (or the second argument for that matter). You need to call germantoenglish recursively to translate the rest of the list. And, as I said, you also need to handle the case where the list is empty.

is list is working as arrays

Prolog lists are linked lists - not arrays. However like arrays they are used to store an arbitrary amounts of data, so they're used for many of the situations where you'd use arrays in imperative langauges. Does that answer your question?

Yes.When I changed like this also it doesn't give any answer.Could you help me

trans([],[]).
trans(e,1).
trans(t,2).
trans(th,3).
germantoenglish([S|H],[G|Ta]):-trans(H,Ta).
englishtogerman([J|Ta],[L|H]):-trans(H,Ta).

Thank you for the guide..................... I made it.

germantoenglish([],[]).
trans(e,1).
trans(t,2).
trans(th,3).
germantoenglish([S|H],[G|Ta]):-trans(S,G),germantoenglish(H,Ta).
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.