tree functions daily exercise: Table

Xah Lee xah at xahlee.org
Sun Jun 12 18:42:58 EDT 2005


Here's the next tree functions exercise in Python, Perl, Java. Other
language solutions welcome.
http://xahlee.org/tree/tree.html
---------------------

 Table('exprString', [iMax]) generates a list of iMax copies of value
of
    eval('exprString'), and returns the refence to the list. i.e.
    [eval('exprString'),eval('exprString'),...]

    Table('exprString', ['i', iMax]) generates a list of the values by
    evaluating 'exprString' when 'i' in the string runs from 1 to iMax.

    Table('exprString', ['i', iMin, iMax]) starts with 'i' = iMin.

    Table('exprString', ['i', iMin, iMax, iStep]) uses steps iStep. If
iStep
    is negative, then the role of iMin and iMax are reversed. Inputs
such as
    [1, -3 , 1] returns bad result.

    Table('exprString', ['i', iMin, iMax, iStep], ['j', jMin, jMax,
iStep],
    ... ) gives a array by iterating 'i', 'j' in 'exprString'. For
example,
    Table('f(i,j)', ['i',1,3], ['j',5,6]) returns [[f(1, 5), f(1, 6)],
[f(2,
    5), f(2, 6)], [f(3, 5), f(3, 6)]].

    In general, Table has the form Table('expressionString', iterator1,
    iterator2, ...) where 'expressionString' is a string that will be
    evaluated by eval. iterator have one of the following forms [iMax],
    ['dummyVarString',iMax], ['dummyVarString',iMin, iMax], or
    ['dummyVarString',iMin, iMax, iStep].

    If Table fails, 0 is returned. Table can fail, for example, when
the
    argument are not appropriate references or the iterator range is
bad
    such as ['i',5,1].

    Example:

     Table('q(s)' ,[3]); # returns ['s','s','s']

     Table( 'i**2' , ['i', 4]); # returns [1, 4, 9, 16]

     Table('[i,j,k]',['i',2],['j',100,200,100],['k',5,6])
     # returns [[[[1,100,5],[1,100,6]],[[1,200,5],[1,200,6]]],
     #          [[[2,100,5],[2,100,6]],[[2,200,5],[2,200,6]]]]


Wolfram Research's Table function documentation is at:
http://documents.wolfram.com/mathematica/functions/Table
(this post is not affliated with Wolfram Research Incorporated and has
not been approved by Wolfram Research Incorporated.)

The first argument of Table function in Mathematica (mma) is a
expression. Most other languages cannot have such symbolic expressions.
In Perl, a string is choosen instead as the experssion, and it is being
evalutade later as code. This may not be a practical choice but anyway
it's just a exercise. Each other language should choose appropriate
design for this emulation...

Perl, Python, Java solutions will be posted by me in the coming days.

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




More information about the Python-list mailing list