在Pharo Smalltalk中进行数组循环 [英] For loop for array in Pharo Smalltalk

查看:65
本文介绍了在Pharo Smalltalk中进行数组循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有随机数(仅0或1)的数组,但是当我运行它时,它只是打印出:遇到语句末尾->

I'm trying to make an array with random numbers (just 0 or 1), but when I run it, it just prints this: End of statement list encountered ->

这是我的代码:

GenList
 | lista |
  lista := Array new: 31.
  1 to: 30 do: [ :i | lista at: i put: 2 atRandom - 1]
  ^lista

我该怎么办?

推荐答案

要考虑的一些有趣的事情:


1.方法选择器不是以小写字母开头

选择器以小写字母开头是一种传统.从这个意义上讲,genListaGenLista更正确.

It is a tradition for selectors to start with a lowercase letter. In this sense, genLista would be more correct than GenLista.

2.方法选择器包括缩写词"gen"

例如,genLista可以重命名为genereLista o listaAlAzar(如果您决定使用西班牙语)

For instance, genLista could be renamed to genereLista o listaAlAzar (if you decide to use Spanish)

3.名为listaArray具有31个元素,而不是30个

3. The Array named lista has 31 elements, not 30

Array new: 31的结果是31个元素的数组.但是,其下面的代码仅填充其中的30个,而最后一个未初始化(即nil).可能的解决方案:lista := Array new: 30.

The result of Array new: 31 is an array of 31 elements. However, the code below it only fills 30 of them, leaving the last one uninitialized (i.e., nil). Possible solution: lista := Array new: 30.

4.缺少一个点导致编译错误

代码

  1 to: 30 do: [ :i | lista at: i put: 2 atRandom - 1]
  ^lista

不编译,因为没有点表示两个句子之间的分隔.请注意,该错误发生在编译时(即,当您保存方法时),因为返回令牌^必须开始一条语句(即,不能在语句内部内联).

does not compile because there is no dot indicating the separation between the two sentences. Note that the error happens at compilation time (i.e., when you save the method) because the return token ^ must start a statement (i.e., it cannot be inlined inside a statement).

在其他情况下,缺少点将不会阻止代码编译.相反,将在运行时发生错误.这是一个(典型)示例:

There are other cases where a missing dot will not prevent the code from compiling. Instead, an error will happen at runtime. Here is a (typical) example:

1 to: 10 do: [:i | self somethingWith: i]     "<- missing dot here"
self somethingElse

缺少点将生成运行时错误self not understood by block.

the missing dot will generate the runtime error self not understood by block.

5.还有一种更具表现力的方式,可以随机生成0和1s

计算2 atRandom - 1可以.但是,它迫使读者在思维上进行数学运算.揭示您意图的更好方法是

The calculation 2 atRandom - 1 is ok. However, it forces the reader to mentally do the math. A better way to reveal your intention would have been

#(0 1) atRandom


6.玩随机数字时,别忘了保存种子

虽然可以使用atRandom,但是这种做法只能与玩具"代码一起使用.如果要开发系统或库,建议的做法是在生成任何随机数据之前将种子保存在某处.这样一来,您便可以在以后进行调试或确认时,重现随机数的生成. (但是请注意,这不足以使您的程序具有确定性的可再现性,因为无序(例如散列)集合在连续执行中可能会形成不同的形式.)

While it is ok to use atRandom, such a practice should only be used with "toy" code. If you are developing a system or a library, the recommended practice is to save the seed somewhere before generating any random data. This will allow you to reproduce the generation of random quantities later on for the sake of debugging or confirmation. (Note however, that this will not suffice for making your program deterministically reproducible because unordered (e.g. hashed) collections could form differently in successive executions.)

这篇关于在Pharo Smalltalk中进行数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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