Re: [eclipse-clp-users] Product of a list (or array)

From: Sergii Dymchenko <kit1980_at_...6...>
Date: Wed, 27 Feb 2013 14:03:41 -0800
Hi,

Is this fast/easy enough for you?

% product(+ExprList, -Result)
product(ExprList, Result) :-
    ( foreach(Expr, ExprList), fromto(1, In, Out, Result) do
        Out is In * eval(Expr) ).

?- product([1, 2, 3], X).
X = 6

?- product([1+1, 2+2, 3+3], X).
X = 48

Sergii.

On Wed, Feb 27, 2013 at 12:50 PM, Martin Michalowski
<martin.michalowski_at_...341...> wrote:
> Is there an equivalent built-in predicate for sum(+ExprList, -Result) that evaluates the arithmetic expressions in ExprList and unifies their product with Result?  If not, do people have a fast/easy way of generating the product of a list?  I know the times predicate is available but I'm hoping to avoid iterating over multiple instances of it.
>
> Best,
> Martin
>
> --
> Martin Michalowski, Ph.D.
> Principal Research Scientist, Adventium Labs
> martin.michalowski_at_...341...
> http://www.adventiumlabs.com/staff/martin-michalowski
>
>
> ------------------------------------------------------------------------------
> Everyone hates slow websites. So do we.
> Make your web apps faster with AppDynamics
> Download AppDynamics Lite for free today:
> http://p.sf.net/sfu/appdyn_d2d_feb
> _______________________________________________
> ECLiPSe-CLP-Users mailing list
> ECLiPSe-CLP-Users_at_lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/eclipse-clp-users
Received on Wed Feb 27 2013 - 22:05:57 CET

This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:20 CEST