Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I use Range a lot. Is there any syntactic shortcut for it, or do you always have to spell it out?

share|improve this question
    
You could use the Notation Package to define a custom shortcurt for it. –  William Briand Oct 20 '13 at 13:41
4  
I think, William means a custom alias using the esc keys. Or you could even define your own shorthand function. R=Range;. You could also try typing R and then pressing Command+(Shift)+K (or the equivalent windows keys) for autocompletion and templates. Yet another option may be to check out Halirutan's IntelliJIdea plugin. –  Timothy Wofford Oct 20 '13 at 13:49
1  
Why don't you try r = Range;? –  Artes Oct 20 '13 at 14:31
add comment

2 Answers

up vote 6 down vote accepted

If you want a nicer layout for Range you could try the Notation package:

Mathematica graphics

Notation is a bit picky about the definition code of your notation. It has to go manually via its templates. That's why I used a picture above. The following code should work when copied:

<< Notation`

CellPrint@Cell[BoxData[
    RowBox[{"InfixNotation", "[", 
       RowBox[{
          TemplateBox[{"\[TripleDot]"},
            "NotationTemplateTag"], ",", "Range"}], "]"}]], "Input"]

After executing those two lines, a third one will appear which must be executed as well. After that you can use the \[TripleDot] character to define ranges:

5 \[TripleDot] 9

{5, 6, 7, 8, 9}

If you just want shorter code you can define:

r = Range;
r@5
r[3, 9, 2]

{1, 2, 3, 4, 5}

{3, 5, 7, 9}

share|improve this answer
    
The Notation package method is very nice. Just a shame that, as things are, you must still either type, literally, "[\TripleDot]" or else create an Escape alias for it. (Unfortunately, Esc ... Esc is already preempted for \[Ellipsis]. –  murray Oct 20 '13 at 22:43
add comment

The reference page for gives variations which utilize default arguments:

Range[20]
Range[1,20]
Range[1,20,1]

An alternative for this usage is Array

Array[#&,20]

If you are after a list of numbers, you could use Table

Range[6,20,2]
Table[i,{i,6,20,2}]

But if you are just using the output of Range as indices for Part then you might want to look at Span

f[[Range[6,20,2]]]
f[[6;;20;;2]]
share|improve this answer
    
Using the shortcut ;; for Span does make code shorter than directly using Range for indexing. But @Sjoerd C. de Vries's method using the Notation package and \[TripleDot] makes indexing expressions more closely resemble traditional mathematical notation. –  murray Oct 20 '13 at 22:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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