我的代码很笨重:
length: length? items
count: 0
foreach item items [
count: count + 1
if count = length [
print "last item"
]
]
还有更好的东西吗?
答案 0 :(得分:1)
通常人们使用FORALL (这将更好地命名为FOR-NEXT)移动系列位置而不是给出项目,然后你可以用TAIL测试它?缺点是您必须从当前位置的系列中选择项目:
forall items [
probe items/1 ;-- how to access current item
if tail? next items [ ;-- could use LAST? ITEMS in Rebol 3
print "last item"
]
]
这大致相当于:
if not tail? items [
original: items
until [
probe items/1
if tail? next items [
print "last item"
]
items: next items
tail? items
]
items: original
]
预先警告:FORALL改变其输入系列并尝试将其放回到最后的初始位置。但是在出现错误的情况下,它的行为定义不明确,因此如果出现问题,您可以将输入保留在迭代中。