# How to I use Map to apply a defined function to a list?

Problem: How to map a function onto a list. My (example input:

mylist = {{1, 2}, {3, 4}}
gg[x_, y_] := {N[x + y], N[x y]}
Map[gg, mylist]


Expected output:

{{3., 2.}, {7., 12.}}


Actual output:

{gg[{1, 2}], gg[{3, 4}]}


How do I get gg[.] to give a numerical (function evaluated) output of mylist?

• try gg[{x_, y_}] := {N[x + y], N[x y]} – user42582 May 9 '18 at 20:48
• gg @@@ mylist. But if you need to use Map then Apply[gg] /@ mylist. – Kuba May 9 '18 at 20:50
• Scan vs. Map vs. Apply - quite broad but should answer. – Kuba May 9 '18 at 20:57
• Not sure why, but that works. Thanks – mark r May 9 '18 at 21:24

As mentioned in the comments by Kuba, you should use

gg @@@ mylist


Indeed, this is precisely raison d'être of @@@. If you want to use Map, then Kuba suggests to use the operator form of @@, to wit,

Apply[gg] /@ mylist


Finally, let me mention that if you insist on using Map, then you should redefine the function gg into (hat tip to kglr)

gg[{x__}] := {N[+x], N[1 x]}


If you do so, then Map[gg, mylist] yields your expected output.

• Fun fact: the expression Plus[x] can be simplified into +x, but Times[x] cannot be replaced by *x. Seems inconsistent to me -- both Plus and Times are OneIdentity, and I'd expect both of them to behave similarly. Wonder why they don't here... – AccidentalFourierTransform May 9 '18 at 22:29
• {+##, 1 ##} & @@@ mylist ? – kglr May 9 '18 at 22:34
• @kglr Ah, perfect. I was actually about to post a question on why * is not allowed to be unary as opposed to +. Your trick works just fine, thanks! – AccidentalFourierTransform May 9 '18 at 22:41
mylist = {{1, 2}, {3, 4}}
gg[x_, y_] := {N[x + y], N[x y]}
Map[gg[#[[1]],#[[2]]]&, mylist]