Using sets of arguments from a list
You could use Apply at the first level:
Times @@@ listA
Short answer: See Heike's post.
Longer version:
There are a couple of functions in the functional programming toolbox that are so common that they've gotten their own symbols. One of them is Apply
, which takes a list and uses its elements as function arguments:
Apply[f, {a, b, c}]
f[a, b, c]
The shorthand notation for this would be f @@ {a, b, c}
.
Additionally, Apply
has an optinal third argument, specifying the depth at which the function should be applied:
(* Apply on 0-th level *)
Apply[f, {{a,b}, {c,d}}]
f[{a,b}, {c,d}]
(* Apply on the first level *)
Apply[f, {{a,b}, {c,d}}, {1}]
{f[a,b], f[c,d]}
The latter is also a very common expression, it has the shorthand notation f @@@ {{a,b}, {c,d}}
. The notation ends here, i.e. there's no @@@@
; if you need that, you'll have to use Apply
explicitly. The documentation features plenty of examples if you need further help.
That said, what you want is converting a list of lists of numbers to a list of the product of the numbers. This is equivalent to applying the product function on the first level of the original list; that function is called Times
in Mathematica (Product
stands for the mathematical expression $\prod_a^bx$). Times
is the internal function that is called when you enter something like a*b*c
: it becomes Times[a,b,c]
internally. Therefore, using the Apply
function from above,
Apply[Times, {{a,b}, {c,d}}, {1}]
(* Evaluates to ... *)
{Times[a,b], Times[c,d]}
(* ... and is equivalent to ... *)
{a b, c d}
That's precisely what you want. Adding the syntactic sugar from above, @@@
, this leaves you with the final short notation
Times @@@ list
to solve the problem.
Just for fun:
Replace[list, {x__} :> 1 x, 1]
Since this answer has garnered some votes here is a more direct form of the implicit Times
:
1##& @@@ list