使用Gimp的方案中的while循环嵌套?

问题描述 投票:1回答: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)))
scheme gimp script-fu
1个回答
2
投票

y永远不会重置回0。您的代码将y递增至35,然后将x递增20次,但是随后每次x递增,y仍设置为35

如果您想遍历xy的每个值组合,那么您将需要更多类似以下的代码:

(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))
    )
  )
)

[在此函数内,我要提取xy的第一个和最后一个值(使用(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 havingy`在函数调用后发生变化。如果像这样调用此函数:

and

您将获得以下输出:

(SO '(15 20) '(30 35))
© www.soinside.com 2019 - 2024. All rights reserved.