0
votes

I am new in Prolog and trying to learn it. I came across a question and I am trying to run it but I think I am making a fundamental mistake and my code does not work properly. I am trying to write a predicate that takes two arguments. The first argument is a list and the second argument is a list which members are the members of the first list, repeated twice.

Here is my code :

twice([],[]).
twice([X|Taila],[X,X|Tailb]) : twice(Taila,Tailb).

For example

twice([z,4,hello],X).

should return

X = [z,z,4,4,hello,hello]).
1
You have a typo. : should be :- in your second clause. - Shon
To find the error: Ask the most general query, like twice(Xs, Ys). And look what you get there. - false
You say your code 'does not work properly'. What does it do? What do you get when asking twice([z,4,hello],X).? And if this is your exact code, as @aBathologist noted, you have a typo. Was that the problem? - SQB
Apart from the typo, your code works as it should. - SQB

1 Answers

0
votes

I got the answer

twice([], []).
twice([X|Taila], [X, X|Tailb]) :- twice(Taila, Tailb).