I'm working on a Prolog application that will go through a list of values and copy the values to another list. If a value is a number, then I want to square that number, otherwise, copy the value.
So given the input:
[k, 4, 9, b]
It should yield:
[k, 16, 81, b]
Here is what I have:
squareMe(X, P) :- P is X * X.
boogie([],[]).
% The next line is where I'm having problems
boogie([X|T], [X|Result]):-
number(X),
boogie(T,Result).
boogie([ThrowAway|Tail], [ThrowAway|Result]):-
boogie(Tail,Result).
Right now, this almost works. So the first thing I tried was:
boogie([X|T], [X*X|Result):-
Prolog would not evaluate the expression because you need "is". So I tried:
boogie([X|T], [Y is X*X|Result):-
That didn't work. I tried:
boogie([k, 4, 9, b], T).
My output was:
T = [k, _G3648 is 4*4, _G3657 is 9*9, b].
Is there a way that I can obtain the square of a number within the list? i.e.
boogie([X|T], [Square of X|Result]):-