Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiply 2 number

Status
Not open for further replies.

nn987

Programmer
Feb 4, 2006
37
GB
how to proced with the empty lists in this case?

%Multiply elements of one list

multLista([],1).
multLista([E|T],R):-multLista(T,Multiplica),R is Multiplica*E.
~

ex:
multLista([1,2,3,4],R).
R=24

multLista([],1).
R= 1 !!!!

Thanks
 

I use this version of prolog

SWI-Prolog version 5.4.7 by Jan Wielemaker (jan@swi-prolog.org)
 
OK thank you.I don't see what your problem is ; perhaps I am missing something.
You have a fact that says

multLista([],1).

then you ask

multLista([],R) ?

and it says R = 1.

Why is that a surprise?

Regards

John
 
Maybe you wish to say that:
% multLista of empty list is 0
multLista([],0).

But than you have to add another edge case so your recursion would work:
multLista([A],A).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top