golfscript嵌套while循环 [英] golfscript nested while loops

查看:109
本文介绍了golfscript嵌套while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

golfscript中嵌套的while循环是否被嵌套,或者我不知道如何使用它们?

Are nested while loops broken in golfscript or do I not know how to use them?

我想将Q从5迭代到0,对于每次迭代,将Z从10迭代到0.单个循环可以很好地独立工作,并且它们似乎是独立的(不依赖于操作之间的堆栈):

I want to iterate Q from 5 to 0, and for each iteration, iterate Z from 10 to 0. The single loops work well separately, and they seem self-contained (not relying on the stack between operations):

5:Q;
{"Q:"Q+ p Q}
{
  Q 1- :Q;
}while

10:Z;{"Z:"Z+ p Z}{Z 1- :Z;}while

输出:

"Q:5"
"Q:4"
"Q:3"
"Q:2"
"Q:1"
"Q:0"
"Z:10"
"Z:9"
"Z:8"
"Z:7"
"Z:6"
"Z:5"
"Z:4"
"Z:3"
"Z:2"
"Z:1"
"Z:0"

但是,如果将Z循环放入Q循环中,则会得到奇怪的结果:

But if I put the Z loop inside the Q loop, I get strange results:

5:Q;
{"Q:"Q+ p Q}
{
  10:Z;{"Z:"Z+ p Z}{Z 1- :Z;}while

  Q 1- :Q;
}while

输出:

"Q:5"
"Z:10"
"Z:9"
"Z:8"
"Z:7"
"Z:6"
"Z:5"
"Z:4"
"Z:3"
"Z:2"
"Z:1"
"Z:0"
"Z:0"

基于Z两次打印,看来当前只有一个条件块,任何执行"while"的操作都会覆盖它.

Based on Z printing out twice, it seems like there is only one current conditional block, and any execution of "while" overwrites it.

无论如何,我将如何在golfscript中完成这项壮举?

In any case, how would I accomplish this feat in golfscript?

推荐答案

不幸的是,看起来(唯一的)解释器无法正确处理嵌套的do/while/until循环.仅当您同时嵌套了两个这样的循环时才出现问题,而当循环的类型不同时则不会出现.

It looks like the (only) interpreter unfortunately doesn't handle nested do/while/until loops correctly. The problem seems to only arise when you have two of these loops of the same time nested, and not when the loops are different types.

例如:

{0do 1}do       #not an infinite loop, but it should be
{0{}while 1}do  #is an infinite loop as expected
{0{"i"p}while 1}{"o"p}while
     #not an infinite loop, outputs "i" instead of continuously outputting "o"

奇怪的是我之前没有注意到这个错误.通常,如果适用,使用构造{ }%{ }/会比使用do/while/until循环更好.对于您的示例,最好使用:

Strangely I haven't noticed this error before. Generally, using the constructs { }% and { }/ will be better than using do/while/until loops if they are applicable. For your example, it would be better to use:

6,-1%{:Q"Q: "\+p
 11,-1%{:Z"Z: "\+p}/
}/

这篇关于golfscript嵌套while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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