1

我正在编写 Gimp Script-Fu 脚本,并尝试使用嵌套的 while 循环。x设置为15y设置为30y循环到35,但x停留在15并且循环退出。这里有什么问题?为什么值x没有改变?

(while (< x 20)
  (while (< y 35)    
    (gimp-message (string-append (number->string x) "-" (number->string y)))
    (set! y (+ y 1)))
  (set! x (+ x 1)))
4

1 回答 1

2

y永远不会被重置回0. 您的代码将递增y35,然后递增x20 次,但是在每个后​​续递增时x y仍设置为35

如果您想查看 的值的每个组合,x那么y您将需要更像这样的代码:

(while (< x 20)
    (set! y 0)
    (while (< y 35)    
            (gimp-message (string-append (number->string x) "-" (number->string y)))
             (set! y (+ y 1))
            )
    (set! x (+ x 1))
)

这是一个更完整的示例,因为我有时间使用 Gimp 解决这个问题(我使用的是print不是gimp-message因为我在控制台中工作,但它应该是可互换的)。首先,我定义了一个调用的函数SO,它接受参数 ,xy它们都表示成对的最小值和最大值:

(define (SO x y)
  (let* ((x! (car x)) (y! (car y)))
    (while (< x! (car (cdr x)))
      (set! y! (car y))
      (while (< y! (car (cdr y)))
        (print (string-append (number->string x!) "-" (number->string y!)))
        (set! y! (+ y! 1))
      )
      (set! x! (+ x! 1))
    )
  )
)

在这个函数中,我提取了 and 的第一个和最后一个值(然后x我使用x! y! x y` 在调用函数后发生变化)。如果你这样调用这个函数:y(car x)(car (cdr x))let* to create two inner variables calledandthat I will be altering the value of (to remove side effects of havingand

(SO '(15 20) '(30 35))

您会得到以下输出:

"15-30"
"15-31"
"15-32"
"15-33"
"15-34"
"16-30"
"16-31"
"16-32"
"16-33"
"16-34"
"17-30"
"17-31"
"17-32"
"17-33"
"17-34"
"18-30"
"18-31"
"18-32"
"18-33"
"18-34"
"19-30"
"19-31"
"19-32"
"19-33"
"19-34"
于 2014-07-15T21:53:54.323 回答