I Tried to write a simple code in Prolog which translate a list to another list. for instance, if we call
L will become
[1,2,3]. (a,b,c is replaced with 1,2,3). But i faced with a syntax error in last line. what is the problem? here is my code:
Best How To :
The error is very likely because in your code:
L1 is declared in the head, but not referenced anywhere: you mispelled something?
Anyway, your code is not going to work.
append/3 for this kind of tasks which are easily defined by recursion is considered terrible (also because of the bad performance you get out of it).
Applying a function to a list is straightforward. You already know that in prolog you don't write
Y = f(X) but rather declare the functional relation between
f(X, Y).. (That's basically what you did with
Now the (easy) recursive formulation:
- the transformed empty list is the empty list
- the transformation of
trans(X,Y) and we recursively transform
or expressed in prolog:
listtrans([X|Xs],[Y|Ys]) :- trans(X,Y), listtrans(Xs,Ys).
I recommend you reading the first 4 chapters of Learn Prolog Now to better understand these concepts.