# Tree Functions: Range

By Xah Lee. Date: . Last updated: .
```Range

Range(‹iMax›) generates the list [1, 2, ... , ‹iMax›].

Range(‹iMin›, ‹iMax›) generates the list [‹iMin›, ... , ‹iMax›].

Range(‹iMin›, ‹iMax›, ‹iStep›) uses increment ‹iStep›, with the last element
in the result being less or equal to ‹iMax›. ‹iStep› cannot be 0. If
‹iStep› is negative, then the role of ‹iMin› and ‹iMax› are reversed.

If Range fails, 0 is returned.

Example:

Range(5); # returns [1,2,3,4,5]

Range(5,10); # returns [5,6,7,8,9,10]

Range( 5, 7, 0.3); # returns [5, 5.3, 5.6, 5.9, 6.2, 6.5, 6.8]

Range( 5, -4, -2); # returns [5,3,1,-1,-3]
```

Mathematica: Range

Solutions:

### perl

```#! perl
# this version is written by tilt...@erols.com (Jay Tilton) , 2005-05-15

sub Range {
my( \$a1, \$b1, \$dx ) =
@_ == 1 ? (    1, \$_[0], 1) :
@_ == 2 ? (\$_[0], \$_[1], 1) :
@_;
if( \$dx == 0 ) {
warn "Range: increment cannot be zero.";
return;
}
return [map \$a1 + \$dx * \$_, 0..int( (\$b1 - \$a1) / \$dx )];
};

##########
# test

use Data::Dumper;
print Dumper(Range(5,-4,-2));
print Dumper(Range2(5,-4,-2));```

### Python

```import math;

def Range(iMin, iMax=None, iStep=None):
if (iMax==None and iStep==None):
return Range(1,iMin)
if iStep==None:
return Range(iMin,iMax,1)
if iMin <= iMax and iStep > 0:
if (isinstance(iStep,int) or isinstance(iStep,long)):
return range( iMin, iMax+1, iStep)
else:
return [ iMin+i*iStep for i in range(int(math.floor((iMax-iMin)/iStep))+1) ]
if iMin >= iMax and iStep < 0:
if (isinstance(iStep,int) or isinstance(iStep,long)):
return range( iMin, iMax-1, iStep)
else:
return [ iMin+i*iStep for i in range(int(math.floor((iMin-iMax)/-iStep))+1) ]
return []

# test
print Range(5,9,-2)```

### scheme lisp

```; scheme

; code by Jussi Piitulainen jpiit...@ling.helsinki.fi 2005-05

;;; Increasing list of b+ks in the interval [b..e) if s > 0.
;;; Decreasing list of b+ks in the interval (e..b] if s < 0.

(define (range b e s)
(do ((k (- (ceiling (/ (- e b) s)) 1) (- k 1))
(r '() (cons (+ b (* k s)) r)))
((< k 0) r)))

; test
(display (range 3 5 1/2))

; Note this code is very much incorrect.```

### Java

```// 2005
import java.util.List;
import java.util.ArrayList;
import java.lang.Math;

class math {
public static List range(double n) {
return range(1,n,1);
}

public static List range(double n, double m) {
return range(n,m,1);
}

public static List range(double iMin, double iMax, double iStep) {
List ll = new ArrayList();
if (iMin <= iMax && iStep > 0) {
for (int i=0; i <= Math.floor((iMax-iMin)/iStep); i++) {
}
return ll;
}
if (iMin >= iMax && iStep < 0) {
for (int i=0; i <= Math.floor((iMin-iMax)/-iStep); i++) {
}
return ll;
}
// need to raise exception here
return ll;
}
}

class Range {
public static void main(String[] arg) {
System.out.println(math.range(5));
System.out.println(math.range(5,10));
System.out.println(math.range(5,7, 0.3));
System.out.println(math.range(5,-4, -2));
}
}```

If you have a question, put \$5 at patreon and message me.