0
$\begingroup$

(If I should elaborate on the circumstances, tell me.)

I want to do the following: Suppose I have a tuple

 tuple={x,4,something,...}

and I want to define a pure function, e.g.

Function[{tuple[[1]]},x^2]

What I expect is that it uses the first element of tuple as the variable in x^2, that is x. However, what I get is that {tuple[[1]]} is not a list of symbols.

(Evaluate around tuple does not help.)

Can someone explain why this does not work and how it would work?

EDIT: So apparently my remark "Evaluate around tuple does not help." was not quite correct. To fix this, one simply has to do Evaluate@{tuple[[1]]} rather than (what I tried first) {Evaluate@tuple[[1]]}

$\endgroup$
8
  • $\begingroup$ Context: I defined a custom Integration that should work with the same syntax as the normal Integrate: cIntegrate[f,{x,x2,x2}], where I need to parse x, the first element of my tuple, as the variable to be integrated. The reason is something about speed of this operation $\endgroup$ Commented Jan 14 at 9:48
  • $\begingroup$ Use this form instead. (#^2)&[tuple[[1]]] The way you did it does not work since tuple[[1]] is not a formal parameter name $\endgroup$
    – Nasser
    Commented Jan 14 at 10:06
  • $\begingroup$ @Nasser But this is backwards from what I'm doing. If I already have my pure Function in the form (#^2)&, I would be done already. The Problem is, that I have an algebraic expression such as "x^2" and I want to convert it to (#^2)& using the built-in Function[]. And the Argument I want it to convert is given by an element of a tuple, i.e. "x" here. $\endgroup$ Commented Jan 14 at 10:32
  • 3
    $\begingroup$ I would write an addendum with the correct answer for other people who may have the same problem. $\endgroup$ Commented Jan 14 at 12:52
  • 1
    $\begingroup$ @Confuse-ray30 You should just answer your own question with an official answer rather than editing your OP with the answer. $\endgroup$
    – march
    Commented Jan 14 at 22:48

1 Answer 1

0
$\begingroup$

So apparently my remark "Evaluate around tuple does not help." was not quite correct. To fix this, one simply has to do Evaluate@{tuple[[1]]} rather than (what I tried first) {Evaluate@tuple[[1]]}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.