我正在编写Gimp Script-Fu脚本,并尝试使用嵌套的while循环。 x
设置为15
,y
设置为30
。 y
循环到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)))
y
永远不会重置回0
。您的代码将y
递增至35
,然后将x
递增20次,但是随后每次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
的函数,该函数接受参数x
和y
,它们均表示最小和最大值对:
(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))
)
)
)
[在此函数内,我要提取x
和y
的第一个和最后一个值(使用(car x)
和(car (cdr x))
,然后使用let* to create two inner variables called
x!and
y![C0 ] xthat I will be altering the value of (to remove side effects of having
y`在函数调用后发生变化。如果像这样调用此函数:
and
您将获得以下输出:
(SO '(15 20) '(30 35))