Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9836 > unrolled thread

Distinquishing #'s in nested pure functions

Started by"Dave Snead" <dsnead6@charter.net>
First post2012-04-27 10:49 +0000
Last post2012-04-28 09:31 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Distinquishing #'s in nested pure functions "Dave Snead" <dsnead6@charter.net> - 2012-04-27 10:49 +0000
    Re: Distinquishing #'s in nested pure functions David Reiss <dbreiss@gmail.com> - 2012-04-28 09:30 +0000
    Re: Distinquishing #'s in nested pure functions A Retey <awnl@gmx-topmail.de> - 2012-04-28 09:31 +0000

#9836 — Distinquishing #'s in nested pure functions

From"Dave Snead" <dsnead6@charter.net>
Date2012-04-27 10:49 +0000
SubjectDistinquishing #'s in nested pure functions
Message-ID<jndtj1$jit$1@smc.vnet.net>
Hi,

Is there a way to distinguish the #'s in nested pure functions?

As a simple example:

f[x_, y_] := x - y

Select[{1, 2, 3}, (f[#, #] & /@ {-1, -2, -3}) == {3, 4, 5} &]

I want the 1st # to correspond with the outer & (the equal)
and the 2nd # with the inner & (the map)
The answer in this example should by {2}
(of course, the statement as written above does not do the job)

Can Mathematica distinguish these #'s?

Thanks in advance,
Dave Snead

[toc] | [next] | [standalone]


#9848

FromDavid Reiss <dbreiss@gmail.com>
Date2012-04-28 09:30 +0000
Message-ID<jngdc5$ot$1@smc.vnet.net>
In reply to#9836
For situations like this I always use explicit calls to Function[...],
though for the innermost our function I may use the shorthand #, &
form.   In addition to avoiding potential ambiguities, it makes the
code readable.

--David


On Apr 27, 6:49 am, "Dave Snead" <dsne...@charter.net> wrote:
> Hi,
>
> Is there a way to distinguish the #'s in nested pure functions?
>
> As a simple example:
>
> f[x_, y_] := x - y
>
> Select[{1, 2, 3}, (f[#, #] & /@ {-1, -2, -3}) == {3, 4, 5} &]
>
> I want the 1st # to correspond with the outer & (the equal)
> and the 2nd # with the inner & (the map)
> The answer in this example should by {2}
> (of course, the statement as written above does not do the job)
>
> Can Mathematica distinguish these #'s?
>
> Thanks in advance,
> Dave Snead

[toc] | [prev] | [next] | [standalone]


#9852

FromA Retey <awnl@gmx-topmail.de>
Date2012-04-28 09:31 +0000
Message-ID<jngde2$p4$1@smc.vnet.net>
In reply to#9836
Hi,

> Is there a way to distinguish the #'s in nested pure functions?
>
> As a simple example:
>
> f[x_, y_] := x - y
>
> Select[{1, 2, 3}, (f[#, #]&  /@ {-1, -2, -3}) == {3, 4, 5}&]
>
> I want the 1st # to correspond with the outer&  (the equal)
> and the 2nd # with the inner&  (the map)
> The answer in this example should by {2}
> (of course, the statement as written above does not do the job)
>
> Can Mathematica distinguish these #'s?

it often does the right thing, but of course there are ambiguities which 
just can't be resolved. What you'd usually do is to use pure functions 
with named arguments, which will resolve these ambiguities. You probably 
should note that the notation with # and & is just a shortcut for 
Function. Look up the documentation of Function for more details. Here 
is an example:

Select[{1, 2, 3},
  Function[outerarg,
   Function[innerarg, f[outerarg, innerarg]] /@ {-1, -2, -3} == {3,
     4, 5}]]

That's of course somewhat more verbose, but you can of course also mix 
those with the shortcut notation, e.g.:

Select[{1, 2, 3},
  Function[outerarg, f[outerarg, #] & /@ {-1, -2, -3} == {3, 4, 5}]]


hth,

albert

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web