How to write a predicate doublelist(L, R) with L is list of numbers, R is also a list and has the same length as L and each element is doubled up.
Example:
?- doublelist([-1, 0, 3], L).
L = [-2, 0, 6].
I’m a prolog beginner, I confuse to write one myself. Please help.
Start by writing down your example:
This is a perfectly valid Prolog. You can use it in all kinds of directions:
So, now you need to make it more general. As a first step, write down what it means:
Next step, substitute variables for numbers:
Can you continue from this? Do you see the pattern?
Now you have a predicate that works for all lists of length 3. Then,