HOW CAN I CACULATE THE SUM OF HUGE LIST OF REALS WITHOUT STACK OVERFLOW .
- abdelrahman
- VIP Member
- Posts: 65
- Joined: 13 Sep 2009 6:23
HOW CAN I CACULATE THE SUM OF HUGE LIST OF REALS WITHOUT STACK OVERFLOW .
My Dear friends
I have a huge list of Reals Like [22222,35555,98989,........].
I would like to calculate the sum of all list Elements ,
when i Use non tail Recursive Procedure i got
Stack overflow Error ??
Any one Can Help me ???
thanks
I have a huge list of Reals Like [22222,35555,98989,........].
I would like to calculate the sum of all list Elements ,
when i Use non tail Recursive Procedure i got
Stack overflow Error ??
Any one Can Help me ???
thanks
- abdelrahman
- VIP Member
- Posts: 65
- Joined: 13 Sep 2009 6:23
this solution worked without stack overflow .. any comment my friends ....
Code: Select all
predicates
% calculate the sum of list of Reals
list_sum:(real* List ,real Start ,real Result)procedure (i,i,o).
clauses
list_sum([],R,R).
list_sum([H|T],Start,Result):-
NewStart=H+Start,
list_sum(T,NewStart,Result).
goal
list_sum(List,0,Result).
abdelrahman
-
- VIP Member
- Posts: 202
- Joined: 6 Mar 2000 0:01
the simplest way I would do this is:
single pass with no backtracking. would handle a list of any size with no memory issues.
Code: Select all
facts
realtotal :(real) :=0.0.
predicates
% calculate the sum of list of Reals
list_sum:(real* List )procedure (i).
clauses
list_sum([]).
list_sum([H|T]):-
realtotal :=H+realtotal ,
list_sum(T).
goal
realtotal:=0.0, % makes sure its starts at 0
list_sum(List),
stdio::write(toString(realtotal)).
AI Rules!
P.
P.
- abdelrahman
- VIP Member
- Posts: 65
- Joined: 13 Sep 2009 6:23
My Dear paul Thank you , but can i use this with wide scope .
My Dear Paul , thank you very much for your sentences ,
but i have an explanation ,
can i use this predicate with wide scope through many classes or
hence it uses object fact , it should be used locally at the scope of current class .
forgive me I am so novice in OOP.
Need more Declaration , thanks for your interest ...
abdelrahman
but i have an explanation ,
can i use this predicate with wide scope through many classes or
hence it uses object fact , it should be used locally at the scope of current class .
forgive me I am so novice in OOP.
Need more Declaration , thanks for your interest ...
abdelrahman
-
- VIP Member
- Posts: 119
- Joined: 31 Mar 2001 23:01
Hi,
Please let me interrupt...
I don't understand why you get a stack overflow with your code. Your code is correct. How many numbers are you adding? Maybe it is the list itself causing the problem, and not the summing. But I have made a list of 10^6 reals with no problem.
Anyway, the following adjustments to Paul's code should work. It is not very nice, but maybe it will avoid the stack overflow.
1) Make the predicate a class predicate. Declare it in the .cl file (and then it can be used anywhere in your program):2) In the pro file:
Try it and see.
cheers
Steve
Please let me interrupt...

I don't understand why you get a stack overflow with your code. Your code is correct. How many numbers are you adding? Maybe it is the list itself causing the problem, and not the summing. But I have made a list of 10^6 reals with no problem.
Anyway, the following adjustments to Paul's code should work. It is not very nice, but maybe it will avoid the stack overflow.
1) Make the predicate a class predicate. Declare it in the .cl file (and then it can be used anywhere in your program):
Code: Select all
predicates
list_sum:(real* List )->real.
Code: Select all
class facts
realtotal :(real) :=0.0.
clauses
list_sum([])=real_total:-!.
list_sum([H|T])=0:-
realtotal :=H+realtotal ,
_=list_sum(T).
cheers
Steve
- abdelrahman
- VIP Member
- Posts: 65
- Joined: 13 Sep 2009 6:23
MY Dear Steve , thank you for your interest ...
My Dear Steve ,
the code i've published here is working and not the code that produces Stack overflow ,
the one that produced stack overflow was non_tail Recursive one . i didn't write it ,
i replaced it with this new one . but i expected that i am novice and you with many other friends are professionals and can write more elegant and effective code .
the global declaration you wrote is the effective one that i am searching for it to use the predicate with wide scope through many classes ...
thank you Very much Steve ..
abdelrahman
the code i've published here is working and not the code that produces Stack overflow ,
the one that produced stack overflow was non_tail Recursive one . i didn't write it ,
i replaced it with this new one . but i expected that i am novice and you with many other friends are professionals and can write more elegant and effective code .
the global declaration you wrote is the effective one that i am searching for it to use the predicate with wide scope through many classes ...
thank you Very much Steve ..
abdelrahman
- Thomas Linder Puls
- VIP Member
- Posts: 1625
- Joined: 28 Feb 2000 0:01
Personally, I am not very happy about using class facts as local temporary variable of a predicate.
So I prefere your own solution.
If you insist on using a modifiable variable, then I would suggest that you use the varM class:
So I prefere your own solution.
If you insist on using a modifiable variable, then I would suggest that you use the varM class:
Code: Select all
clauses
list_sum(L) = Sum:value :-
Sum = varM::new(0),
foreach V = list::getMember_nd(L) do
Sum:value := Sum:value+V
end foreach.
Regards Thomas Linder Puls
PDC
PDC
-
- VIP Member
- Posts: 202
- Joined: 6 Mar 2000 0:01
-
- VIP Member
- Posts: 119
- Joined: 31 Mar 2001 23:01
- abdelrahman
- VIP Member
- Posts: 65
- Joined: 13 Sep 2009 6:23
My Dear Thomas , thanks .
My Dear Thomas , thanks for your New Idea ,
actually by Trial , unfortunately Steve's code also produced a stack over flow ,,,
so i returned to the working Tail recursive code .
I didn't try your code yet and Because I didn't use the varM Class Before ,studying For Each , hence
your code needs me more time to understand ...
on my opinion , I think simple ideas directly using variables are much more easier when works efficiently .
Again ,
Thanks ...
actually by Trial , unfortunately Steve's code also produced a stack over flow ,,,
so i returned to the working Tail recursive code .
I didn't try your code yet and Because I didn't use the varM Class Before ,studying For Each , hence
your code needs me more time to understand ...
on my opinion , I think simple ideas directly using variables are much more easier when works efficiently .
Again ,
Thanks ...
Another way: list::forAll() is the same foreach...getMember_nd loop.
Code: Select all
clauses
list_sum(L) = Sum:value :-
Sum = varM::new(0),
list::forAll(L,{(V):- Sum:value := Sum:value+V}).
-
- VIP Member
- Posts: 202
- Joined: 6 Mar 2000 0:01
- Thomas Linder Puls
- VIP Member
- Posts: 1625
- Joined: 28 Feb 2000 0:01
-
- VIP Member
- Posts: 202
- Joined: 6 Mar 2000 0:01