Re: [eclipse-clp-users] A problem I am facing in Eclipse prolog

From: Joachim Schimpf <joachim.schimpf_at_...44...>
Date: Mon, 08 Jun 2009 15:48:04 +1000
Yahya Mowiena wrote:
> Dear Eclipse-Prolog users
> 
> I am having a problem of getting a variable value out of the foreach loop
> here's my code
> 
> (foreach(city_windArea{number_built: Ni}, WindACVars) do
>         (foreach(Ni, List), param(Ni,List,Sum) do true),
>          sum1(List, Sum)
> 
> ).

I assume you have a list of city_windArea structures, and you want
to make a list of their number_built fields.

You do that using a single loop with 2 foreachs.  After the loop, you can
use the new list:

  ( foreach(city_windArea{number_built: Ni}, WindACVars),
    foreach(Ni, List) do
       true
  ),
  % now we are ready to use List


> 
> %Cost $= 2000000 * Sum.
> 
> 
> 
> sum1(List, Sum):-
>           (foreach(X,List),
>               fromto(0, In, Out, Sum) do
>                       Out $= In + X
>           ).


You don't need the sum1 auxiliary.  Just write directly

   Cost $= 2000000 * sum(List).



> 
> I want to retrieve the sum calculated in the foreach loop to calculate 
> the cost but when I uncomment the cost function, an error appears
> 
> any help is highly appreciated
> 
> Thanks a lot
> Best Regards
> 
> mowiena@...6... <mailto:mowiena@...6...>
> 
> -- 
> Yahya Mowiena
Received on Mon Jun 08 2009 - 05:48:19 CEST

This archive was generated by hypermail 2.2.0 : Mon Jul 09 2018 - 02:05:29 CEST