tree functions daily exercise: Range

Xah Lee xah at xahlee.org
Sun Jun 12 18:03:30 EDT 2005


Here's the belated Java solution.

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++) {
                ll.add(new Double(iMin+i*iStep));
            }
            return ll;
        }
        if (iMin >= iMax && iStep < 0) {
            for (int i=0; i <= Math.floor((iMin-iMax)/-iStep); i++) {
                ll.add(new Double(iMin+i*iStep));
            }
            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));
    }
}

Perl & Python versions archived here:
http://xahlee.org/tree/tree.html

 Xah
 xah at xahlee.orghttp://xahlee.org/


Dear functional programers,

i run services called a-python-a-day and a-java-a-day, where each day i

 give a tip or snippet of code in these languages, also as a way for me

 to learn these languages.

I've been running this perl-python a-day mailing list for several
 months, and have accumulated a sizable tutorial here:
http://xahlee.org/perl-python/python.html

In the coming days, i'll be starting to translate a complete set of
 tree processing functions. These functions, always takes a tree as
 input (arbitrary nested list) and always returns a list of a definite
 structure. Together, they form a particular functional programing
 paradigm. See this page for the complete documentation as it exists
 now:
http://xahlee.org/PerlMathemat ica_dir/Matica.html

As usuall, each day we will be spending about 20 minutes enjoying these

 little exercises. If past experience is a good indication, then i
 presume that in a month or two we will have built a complete set of
 functions that manipulates tress, in languages Python and Java and
 Perl. (the Perl set is already written)

I'm always interested in LISP and Haskell languages, but never
 seriously learned them. I'm hoping that you functional programers in
 these languages or learners, join me in these 15 minutes daily
 exercises as a fun routine to learn languages or functional
programing.

I'm in particular very interested in the daily discussions of these
 topics by functional programers, as well seeing such set of complete
 code in scsh and Haskell. (Common Lisp or other functional languages
 are always welcome of course)

  Xah 
   x... at xahlee.orghttp://xahlee.org/




More information about the Python-list mailing list