Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Functional Expression Meaning (was:A Functional Expression Trick)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23934] Re: Functional Expression Meaning (was:[mg23859] A Functional Expression Trick)
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Fri, 16 Jun 2000 00:57:17 -0400 (EDT)
  • References: <NDBBJGNHKLMPLILOIPPOIEEHCBAA.djmp@earthlink.net>
  • Sender: owner-wri-mathgroup at wolfram.com

David,
Thanks for the amendment.
It looks though as if the extra rule needs to be in parallel with the old
one and not after it:

toFunc::usage =  "toFunc[expr] will convert an expression of pure functions
into a pure function.";

OLD FORM

toFunc[expr_] :=
  ReleaseHold[
    Function @@ ({expr} /. (F_ /; F === Function)[vars_, body_] :>
              Apply[F[vars, Hold[body]],
                Slot /@ Range[Last[Dimensions[Unevaluated[{vars}]]]]] /. (F_
/;
                   F === Function)[body_] :> Hold[body])]

toFunc[Function[x, Function[#^2 + x]]]

        Hold[#1^2 + #1] &

toFunc[ Function[2# + Function[x, x^2]]]

        2*#1 + (Function[x, Hold[x^2]]) @@
           Slot /@ Range[Last[Dimensions[Unevaluated[{x}]]]] &


NEW FORM

toFunc[expr_] := ReleaseHold[Function @@ ({expr} /. {
            (F_ /; F === Function)[vars_, body_] :>
              Apply[F[vars, Hold[body]],
                Slot /@ Range[Last[Dimensions[Unevaluated[{vars}]]]]],
            Function[body_] :> Hold[body]
            }
        )
    ]

toFunc[Function[x, Function[#^2 + x]]]

        (#1^2 + #1 & ) &

toFunc[ Function[2# + Function[x, x^2]]]

        2*#1 + (Function[x, x^2]) &

Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

----- Original Message -----
From: "David Park" <djmp at earthlink.net>
To: mathgroup at smc.vnet.net
"Allan Hayes" <hay at haystack.demon.co.uk>
Subject: [mg23934] RE: Functional Expression Meaning (was:[mg23859] A Functional
Expression Trick)


>
>
> > -----Original Message-----
> > From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> > Dear David and Hartmut,
> > I have come across some problems with Hartmut's solution, toFunc,
> > as written
> > up by David.
> > I offer new version, toFunc2
> >
>
> Dear Allan and Hartmut,
>
> Allan's solution is more robust than Hartmut's, but it leaves out the case
> of pure functions in short form, using slot numbers directly. So I added
an
> extra rule for that case. Here is it:
>
> toFunc::usage =
>     "toFunc[expr] will convert an expression of pure functions into a pure
\
> function.";
> toFunc[expr_] :=
>   ReleaseHold[Function @@ ({expr} /.
>           (F_ /; F === Function)[vars_, body_] :>
>             Apply[F[vars, Hold[body]],
>               Slot /@ Range[Last[Dimensions[Unevaluated[{vars}]]]]
>                 ]
>           /. (F_ /; F === Function)[ body_] :> Hold[body]
>         )]
>
> I think this is a very useful routine. Thanks to both of you.
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>



  • Prev by Date: RE: Sums and Products: Compact Notation and Differentiation
  • Next by Date: Re: System of lorenz equations
  • Previous by thread: RE: Functional Expression Meaning (was:A Functional Expression Trick)
  • Next by thread: Re: Functional Expression Meaning (was:A Functional Expression Trick)