Reputation: 35
Having - myMultiply/3
myMultiply([],X,[]) :- !.
myMultiply([H|T],X,Result) :-
myMultiply(T,X,NewRest),
Elem is H*X,
Result = [Elem|NewRest].
Which which mulply each element at the left list to the nun at 2nd arg and return Result
.
For example -
myMultiply([1,4,5],4,R).
R = [4,16,20].
And tlm/3
tlm(List1,[],_) :- !.
tlm(List1,[H2,Rest2],Result):-
myMultiply(List1,H2, Elem),
tlm(List1,Rest2,NewResult),
Result = [Elem|NewResult].
Which there I trying to make myMultiply/3
to the left list on each element of the right list and retun Result
.
For example -
?- tlm([3,4,2],[4,7,8],R).
R = [[12,16,8],[21,28,14],[24,32,16]].
Generally the idea is the get the Elem
list and concat him to NewResult
.
My problem is that tlm/3
returns false
-
?- tlm([3,4,2],[4,7,8],R).
false.
Instead of the above example .
What I did wrong here ?
Upvotes: 0
Views: 546
Reputation: 60004
There are 2 problems with your code.
Here I've done a basic correction, and commented about useless cuts.
myMultiply([],X,[]) :- !. % the cut is useless here
myMultiply([H|T],X,Result) :-
myMultiply(T,X,NewRest),
Elem is H*X,
Result = [Elem|NewRest].
tlm(List1,[],[]) :- !. % the cut is useless here
tlm(List1,[H2|Rest2],Result):-
myMultiply(List1,H2, Elem),
tlm(List1,Rest2,NewResult),
Result = [Elem|NewResult].
and the result is...
?- tlm([3,4,2],[4,7,8],R).
R = [[12, 16, 8], [21, 28, 14], [24, 32, 16]].
Could you spot why cuts are useless?
Upvotes: 1