2
$\begingroup$

Evaluating

     Range[1, n] /. n -> 3

produces an error message

 range::range: Range specification in Range[1,n] does not have appropriate bounds

even though it does produce correct output {1, 2, 3}.

What's a good way to accomplish the same thing while avoiding the error message?

$\endgroup$
8
  • 4
    $\begingroup$ ReplaceAll or /. does not hold its arguments, so Range[1, n] is evaluated with symbolic n first. $\endgroup$
    – ilian
    Sep 22 '15 at 19:29
  • $\begingroup$ Because it first evaluates Range[1,n] which is not very miningful and only then substitutes n->3 With[{n=3},Range[1,n]] could be more appropriate or R[n_]:=Range[1,n]; R[3] or as suggested by @ilian $\endgroup$ Sep 22 '15 at 19:32
  • $\begingroup$ Yet Attributes[ReplaceAll] gives no Hold attributes! $\endgroup$
    – murray
    Sep 22 '15 at 19:57
  • $\begingroup$ @murray that is exactly what illian says... $\endgroup$ Sep 22 '15 at 20:19
  • $\begingroup$ Sorry, I missed the "no" in illian's comment. All is clear now, thanks. $\endgroup$
    – murray
    Sep 22 '15 at 21:22
2
$\begingroup$

For your updated question I think you want to hold evaluation until all arguments are numeric.

myRange[x__?NumericQ] := Range[x]

myRange[1, n] /. n -> 3
{1, 2, 3}
$\endgroup$

Your Answer

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

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