1
$\begingroup$

This is a style question I suppose, not sure.

Ok so I have this:

a := RandomInteger[{1, 10}];
f[a_] := a - a
f[a]
0

its ofc always going to be 0 because the 'a'-s are the same, how do I make it such that "some" 'a'-s are not the same, inside the function (I'm not sure how to explain this so bear with me)?

one way to do it ofc is just this:

a := RandomInteger[{1, 10}];
f[a_] := a - RandomInteger[{1, 10}]
f[a]
2

so now the first 'a' is the function call value of the first (and only, in this case) argument, the second 'a' is just evaluated on-the-spot and is not the same as the called 'a' (well it can be, there is a 10% chance in this case but hopefully you see what I mean)

However in this case its not immediately apparent that this second 'term' is of "type a" since its specifically restated into RandomInteger (or whatever 'a' would be, its just an example).

I am basically trying to make some kind of two different 'types' of 'a' in this case, 1 that is evaluated on function call and then another that is evaluated separately, inside the function call of the first one, if this makes sense.

Perhaps to be clearer, the second case with just using RandomInteger instead of 'a' is all fine and dandy, but it "doesn't look good" (whatever that means) I want it to have some sort of code-relation to the actual 'a' variable, that is all. So for instance if I defined a1=RandomInteger and then use that as the 'second a', its not good because now that is a new variable with no apparent connection to 'a'.

$\endgroup$
5
  • $\begingroup$ You could do something like aList = Table[a, {2}] and then evaluate aList[[1]] - aList[[2]]. $\endgroup$
    – J. M.'s torpor
    Jan 30 at 19:54
  • 1
    $\begingroup$ In this particular case, the simplest is probably to give f one of the Hold - attributes. For example, SetAttributes[f, HoldFirst]. This will make your original example work. $\endgroup$ Jan 30 at 21:10
  • $\begingroup$ Okay, but HoldFirst seems to be extremely... Esoteric? Theres no HoldLast or "hold n-th" kind of function so I suppose Id have to construct my own If I want to have a mix of such pre-evaluated and after-evaluated same arguments? $\endgroup$
    – redivider
    Jan 31 at 13:39
  • $\begingroup$ Hold attributes are not as esoteric as you may think. If you want a mix, declare your function HoldAll, but wrap the ones you want evaluated, in Evaluate, when passing to the function. You can read more about Hold* attributes e.g. here, or in this Q / A. Usually Hold* attributes are used to emulate pass by reference semantics and allow a function to modify its argument, but in general one can use them to pass unevaluated piece of code to a function, for whatever purpose. $\endgroup$ Jan 31 at 16:55
  • $\begingroup$ Another way, which does not involve assigning Hold attributes, would be to wrap the parameters you don't want to evaluate, in Unevaluated wrapper. You can think of Unevaluated as a one-time Hold attribute applied for a particular argument in a particular function call. You can read more on Unevaluated e.g. here and here. $\endgroup$ Jan 31 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.