0

So this is my code base structure currently.

trans([], []).
trans([H|T], [NewH|NewT]):-
   means(H, NewH),
   trans(T,NewT).

means(one, uno) :- !.
means(two, dos) :- !.
means(X, X) :- !.

Question1

This works roughly the way I want it to. For example, if I type in the prolog compiler:

?- trans([bob, uno, dos], X).   
   X = [bob, uno, dos].

It answers correctly. However if I insert an argument with a capital letter for example:

?- trans([Bob, uno, dos], X).

It throws a bunch of errors for some reason. How can I fix it so this doesn't happen?

Question2

Currently it returns the answer into a List of several words e.g (trans([bob, one, two], X). will return X = [bob, uno, dos].

How can I make it so it returns a list of my words connected? (Like this: X = [bob uno dos] )

false
  • 10,264
  • 13
  • 101
  • 209
Mbo1020
  • 11
  • 2
  • Another issue: `trans([one],[one])` succeeds. So is one the translation of one? Certainly not. Instead of using cuts, consider putting `dif/2` goals into the last clause. – false Jan 02 '20 at 19:58

2 Answers2

1

Q1: uno is an atom. To make Bob an atom, use single quotes. Thus write 'Bob'. See this answer for more.

However, I cannot reproduce your claim that

It throws a bunch of errors for some reason.

You need to give the true transscript for this.

Q2: Why do you need this? Most probably it is preferable to stick to Prolog syntax. There is atom_chars/2, , and atom_concat/3 for this. But still, I believe you are needlessly transforming things.

false
  • 10,264
  • 13
  • 101
  • 209
  • Oh looks like the error was caused due to a small mistake I made using the sicstus software. it looks like it works if correctly if I use 'Bob' but if I use it without quotes it thinks that Bob=one for some reason and it translates it to uno. So [Bob, one, two] returns [uno, uno, dos] – Mbo1020 Jan 02 '20 at 19:12
  • RIght, `Bob` is a variable, and `'Bob'` an atom. – false Jan 02 '20 at 19:54
0

If something starts with a capital letter in Prolog it always is interpreted as an Variable. That's just the syntax but you could work with strings. So

trans(["Bob", "uno", "dos"], X).

That should also solve your second question if I get it right because then you want it to return

X = "Bob uno dos"

man zet
  • 826
  • 9
  • 26