Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

adding numbers
how can I add numbers define by facts?

eg.

n(1).
n(3).
n(5).
n(6).

add(N) :-
n(X),
N is N + X,
fail.


Does not seem to work...the answer I am looking for should be 1 + 3 + 5 + 6 
= 15.

Report this thread to moderator Post Follow-up to this message
Old Post
Nelson Marcelino
12-04-04 02:01 AM


Re: adding numbers
Nelson Marcelino wrote:

> how can I add numbers define by facts?

Collect all answers into one list and then compute the sum of its elements:

n(1).
n(3).
n(5).
n(6).


list_sum(L,Sum) :-
list_sum(L,0,Sum).

list_sum([],S,S).
list_sum([E|Es],S1,S) :-
S2 is S1 + E,
list_sum(Es,S2,S).


thesum(Sum) :-
bagof(N,n(N),List),
list_sum(List,Sum).


Best regards,
Markus Triska.

Report this thread to moderator Post Follow-up to this message
Old Post
Markus Triska
12-07-04 09:13 AM


Re: adding numbers
Thanks for the reply Markus,

Just wondering is it possible to compute a summation using a recursive
predicate definition
without having to resort to the use of Lists?

Your help is greatly appreciated.

PS I'm finding prolog to be a better tool for many things than lisp,
or functional languages, such
as Ocaml, Haskell, SML, clean,etc.

Currently I am using SWI Prolog.
Can you recommend a good prolog for production grade code. SWI seems
like a very good product.
Best regards,

Nelson Marcelino

Markus Triska <triska@gmx.at> wrote in message news:<41b1cd63$0$11352$3b214f66@tunews.univi
e.ac.at>...
> Nelson Marcelino wrote:
> 
>
> Collect all answers into one list and then compute the sum of its elements
:
>
> n(1).
> n(3).
> n(5).
> n(6).
>
>
> list_sum(L,Sum) :-
> 	list_sum(L,0,Sum).
>
> list_sum([],S,S).
> list_sum([E|Es],S1,S) :-
> 	S2 is S1 + E,
> 	list_sum(Es,S2,S).
>
>
> thesum(Sum) :-
> 	bagof(N,n(N),List),
> 	list_sum(List,Sum).
>
>
> Best regards,
> Markus Triska.

Report this thread to moderator Post Follow-up to this message
Old Post
Nelson Marcelino
12-07-04 09:13 AM


Re: adding numbers
Nelson Marcelino wrote:

>
> Just wondering is it possible to compute a summation using a recursive
> predicate definition
> without having to resort to the use of Lists?
>

Sure, it's only a matter of convenience. If for example you are using
trees to represent sums,:

treesum(nil,0).
treesum(tr(Val,Left,Right),Sum) :-
treesum(Left,L),
treesum(Right,R),
Sum is L + R + Val.

However, in your case I see no advantage in avoiding bagof/3. (You can
surely rewrite the predicate yourself using forced backtracking and such.)

> Currently I am using SWI Prolog.
> Can you recommend a good prolog for production grade code. SWI seems
> like a very good product.

I have no experience with commercial implementations, so I'm afraid I
can't help you with these (I heard that SICStus is very good).
Personally, I'm mainly using SWI Prolog and find it a pleasure to work with.

Best regards,
Markus.

Report this thread to moderator Post Follow-up to this message
Old Post
Markus Triska
12-07-04 09:13 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:06 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.