Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a function to a module without specifying its arguments

I want to write a

 Module Arg[f_,n_] 

that takes a function f (having <=n arguments) and a natural number n and outputs the n-th argument of the function f.

As an example, suppose that f is defined by

f[a_,b_]=a^2+b^2. 

Then,

Arg[f[s,t],1] 

should be s;

while

Arg[f[u,v],2] 

should be v.

My question is whether this is possible. If so, what should I write in the place of "???" below?

Arg[f_,n_] := Module[{}, ??? ]

Note that I don't want to specify a_ and b_ in the definition of Arg like

 Arg[f_,a_,b_,n_]

EDIT: "Arg" is just my name for the module not the internal function Arg of Mathematica.

like image 965
Cantor Avatar asked Nov 19 '11 23:11

Cantor


People also ask

How do you call a function without passing arguments?

to answer your question concerning calling functions without their parameters. it's possible for functions where default values for all parameters are set e.g.: def foo(bar = "default string"): print(bar) print(foo()) # prints: default string print(foo("hello world!")) # prints: hello world!

Can a function be called without arguments?

You can use a default argument in Python if you wish to call your function without passing parameters. The function parameter takes the default value if the parameter is not supplied during the function call.

How do you call a function without an argument in Python?

Defining a Function Without Parameters We can call the function by typing its name followed by parentheses () . When we call this function, it will print the current date. Note that, this output can be different for you. The output will be the date in which you call the function.

How do you call a function without using the function name to send parameters?

How to call a function without using the function name to send parameters? Explanation: None.


1 Answers

Perhaps

SetAttributes[arg, HoldFirst];
arg[f_[x___], n_] := {x}[[n]]

f[a_, b_] := a^2 + b^2.
arg[f[arg[f[s, t], 1], t], 1]
arg[f[s, t], 2]

(*
 -> s
 -> t
*)

arg[ArcTan[f[Cos@Sin@x, x], t], 1]

(*
->  x^2. + Cos[Sin[x]]^2
*)
like image 90
Dr. belisarius Avatar answered Jan 02 '23 19:01

Dr. belisarius