2

I'm trying to understand the Map and Apply notation and ran into a problem.

I define this function:

f23 = Function[x, x^2];
Map[f23, {1, 2, 4, 6, 5, 8}]

and correctly get

{1, 4, 16, 36, 25, 64}

or

f23[{1, 4, 16, 36, 25, 64}]

yields

{1, 4, 16, 36, 25, 64}

but if I use Apply

Apply[f23, {12, 2, 4, 6, 5, 8}]

I only get

144

the first element. If I try using @@ it behaves just as Apply as expected.

So if I understand why apply is only operating on the first element, I can perhaps continue to learn how to use @@

Update: f23@{12, 2, 4, 6, 5, 8}

gets me

{144, 4, 16, 36, 25, 64}

So it appears I don't understand the apply @@ function

Tom Mozdzen
  • 521
  • 5
  • 16
  • 1
    It is not Apply, your function is defined to care about the first argument only. Confusing part is that x^2 threads over lists that is why f23[{..}] returns a list. You can Trace to check. – Kuba Jun 17 '17 at 06:26
  • 1
    Aside a simple mistake with argument you can find answers in: Scan vs. Map vs. Apply. Is it enough? – Kuba Jun 17 '17 at 06:28
  • @Kuba, I've got to read up on what a "head" is. Might make sense then, but thanks for the link – Tom Mozdzen Jun 17 '17 at 06:39
  • @kuba - can you give me an example of how I'd make the function care about more than just the first argument? I do see your point, but don't know how to extend that function in the definition. – Tom Mozdzen Jun 17 '17 at 06:40
  • @kuba - The link does explain to me how to use Apply - very powerful. It is very educational and the link on that page by Leonid S. is very helpful too. http://www.mathprogramming-intro.org/book/node53.html - post as an answer and I'll mark it accepted. – Tom Mozdzen Jun 17 '17 at 06:51
  • 1
    Take a look at documentation of Function, (Function[{u, v}, u^2 + v^4]), then SetDelayed, Listable etc. – Kuba Jun 17 '17 at 06:57
  • 1
    Here is another one: Can a function be made to accept a variable amount of inputs?. If those two linked topics are not exhausting the subject please try to rephrase the question, if they do I will link them as duplicates. – Kuba Jun 17 '17 at 06:59
  • I do like the 2nd link you gave me slightly better than the 1st and a little easier to understand. However, I like the link inside the first link that points to Leonid's chapter on higher order functions which I found was the easiest to understand. I'd say the 2 links are complementary. – Tom Mozdzen Jun 17 '17 at 07:10
  • Ok, since it wasn't a clear case so we don't have to close, the more that m_goldberg summarized it nicely. – Kuba Jun 17 '17 at 07:54

1 Answers1

4

Actually, your problem here is that you don't fully understand Function. The 2nd bullet point under Details in Function says

If there are more arguments supplied than [used by] the function, the remaining arguments are ignored. 

Let's trace the evaluation of Function[x, x^2] @@ {12, 2, 4}

Trace[Function[x, x^2] @@ {12, 2, 4}]

{Function[x, x^2] @@ {12, 2, 4}, Function[x, x^2][12, 2, 4], 12^2, 144}

We see that Apply changes the head List to Function[x, x^2] just as expected. Then Function gets the three arguments 12, 2, 4 and ignores all but the 1st as documented.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • Ah - thank you. So when I do this: Function[x,x^2]@@{{12,2,4}}, I now get {144,4,16}. Not that the double brackets are useful, but confirms what one would expect. – Tom Mozdzen Jun 17 '17 at 21:06