树功能日常锻炼:范围 [英] tree functions daily exercise: Range

查看:59
本文介绍了树功能日常锻炼:范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是迟来的Java解决方案。


import java.util.List;

import java.util.ArrayList;

import java.lang.Math;


class math {

public static List range(double n){

返回范围(1,n,1);

}


公共静态列表范围(双n,双m){

返回范围(n,m,1);

}


公共静态列表范围(双iMin,双iMax,双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));

}

返回ll;

}

if(iMin> = iMax&& iStep< 0) {

for(int i = 0; i< = Math.floor((iMin-iMax)/ - iStep); i ++){

ll.add(new双倍(iMin + i * iStep));

}

返回ll;

}

//需要在这里提出例外

返回ll;

}

}


类范围{

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版本:
http://xahlee.org/tree/tree .html


Xah
xa*@xahlee.org

a ?? http://xahlee.org/

亲爱的功能编程人员,


i运行服务称为a-python-a-day和a-java-a-day,每天我都会给你一个提示或者提示这些语言的代码片段,对我来说也是一种方式


来学习这些语言。


我一直在运行这个perl -py $ a $ day个月的邮件列表,并在这里累积了一个相当大的教程:
http://xahlee.org/perl-python/python.html


在未来的日子里,我''将开始翻译一整套

树处理函数。这些函数总是将树作为

输入(任意嵌套列表)并始终返回一个明确的

结构的列表。它们共同构成了一个特定的功能性编程

范例。请参阅此页面以获取完整的文档

now:
http://xahlee.org/PerlMathemat ica_dir / Matica.html


我们每天都会花20分钟左右享受这些


小练习。如果过去的经验是一个很好的指示,那么我认为在一两个月内我们将构建一套完整的

函数来操纵tress,用Python语言和Java和

Perl。 (Perl集已经写好了)


我总是对LISP和Haskell语言感兴趣,但从来没有认真学过它们。我希望你能够在这些语言或学习者中使用这些语言或学习者,每天15分钟和我一起练习

练习作为学习语言或功能的有趣例程/>
编程。


我特别对功能程序员对这些

主题的日常讨论非常感兴趣,以及看到scsh和Haskell中这样一套完整的

代码。 (Common Lisp或其他功能语言

当然是受欢迎的)


?* Xah

?* x ... @ xahlee.org

a ?? http://xahlee.org/

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
xa*@xahlee.org
a?? http://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...@xahlee.org
a?? http://xahlee.org/

推荐答案

这里是Python,Perl,Java中的下一个树函数练习。其他

语言解决方案欢迎。
http:/ /xahlee.org/tree/tree.html

---------------------


Table(''exprString'',[iMax])生成一个价值的iMax副本列表



eval(''exprString'') ,并将refence返回到列表。即

[eval(''exprString''),eval(''exprString''),...]


表(''exprString' ',[''我',iMax])当字符串中的''i'从1运行到iMax时,通过

评估''exprString''生成一个值列表。 />

表(''exprString'',[''我',iMin,iMax])以''i'= iMin开头。


Table(''exprString'',[''i'',iMin,iMax,iStep])使用步骤iStep。如果

iStep

为负数,则iMin和iMax的作用相反。输入



[1,-3,1]返回不良结果。


表(''exprString'' ,[''我',iMin,iMax,iStep],[''j'',jMin,jMax,

iStep],

...)给出通过在''exprString''中迭代''i'',''j''的数组。对于

例子,

表(''f(i,j)'',[''我',1,3],[''j'' ,5,6])返回[[f(1,5),f(1,6)],

[f(2,

5),f( 2,6)],[f(3,5),f(3,6)]]。


一般来说,Table的形式为Table(''expressionString'', iterator1,

iterator2,...)其中''expressionString''是一个字符串,它将由eval评估为
。迭代器具有以下形式之一[iMax],

[''dummyVarString'',iMax],[''dummyVarString'',iMin,iMax]或

[''dummyVarString'',iMin,iMax,iStep]。


如果表失败,则返回0。表可能会失败,例如,



参数不适合引用或迭代器范围是

bad

,如[''我',5,1]。


示例:


表(''q(s) '',[3]); #wurn ['s'','s'','s'']


表(''i ** 2'',[''我'' ,4]); #report [1,4,9,16]


表(''[i,j,k]'',[''i'',2],['' j'',100,200,100],[''k'',5,6])

#返回[[[[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的表函数文档位于:
http://documents.wolfram.com/mathema...unctions/Table

(这篇文章与Wolfram Research Incorporated没有关系并且已经

未经Wolfram Research Incorporated批准。)


Mathematica(mma)中Table函数的第一个参数是

表达式。大多数其他语言都不能有这样的符号表达。

在Perl中,字符串被选择作为experssion,并且它后来作为代码被b / b
evalutade。这可能不是一个实际的选择,但无论如何

它只是一个练习。对于这种模拟,每种其他语言都应该选择合适的

设计...

Perl,Python,Java解决方案将在未来几天由我发布。


Xah
xa*@xahlee.org

a? ? http://xahlee.org/

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/mathema...unctions/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
xa*@xahlee.org
a?? http://xahlee.org/


感谢


发布了Tree函数的Perl版本。它有点长。

请看这里的代码:
http://xahlee.org/tree/Table.html


选择将字符串作为Table的第一个参数有点<在Perl中,
尴尬。可能我将不得不重写它,以便第一个

参数是一个函数,在每次迭代中,

变量被输入函数。这必然会破坏Mathematica的表格的语法形式,并且稍微不那么灵活

的权力,但在非符号语言中更自然和实用

像Perl,Python,Java。


我认为整个树函数的目标是项目
http://xahlee.org/tree/tree.html

会使代码真正实际上用于处理每种语言的b / b
,而不是坚持使用这些函数的统一性

跨语言。


稍后,当我们编写更多的树函数时,整个集合将非常有用,可用于处理树结构,例如XML和许多其他的东西今天就会从它中抛出来。


免责声明:此项目与Wolfram Research Inc.无关。


Xah
xa*@xahlee.org

a ?? http://xahlee.org/

The Perl version of the Tree function is posted. It''s a bit long.
Please see the code here:
http://xahlee.org/tree/Table.html

the choice of having a string as the first argument to Table is a bit
awkward in Perl. Possibly i''ll have to rewrite it so that the first
argument is a function instead, where in each iteration the the
variables are fed to the function. This necessarily breaks the
Mathematica''s syntactical form of Table, and is slightly less flexible
in power, but is more natural and practical in non-symbolic languages
like Perl, Python, Java.

I think the goal of the whole tree functions project
http://xahlee.org/tree/tree.html
would make the code actually practically useful for processing trees in
each language, as opposed to sticking to uniformness of these functions
across languages.

later on, as we write more tree functions, the whole set will be very
useful in processing tree structures, such as XML and many other things
spurn from it today.

Disclaimer: this project is not affiliated with Wolfram Research Inc.

Xah
xa*@xahlee.org
a?? http://xahlee.org/


这篇关于树功能日常锻炼:范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆