我想在方案语言中做一个简单的计数器增加一个,但是我对语言并不那么熟悉,并且尝试过许多脚本而没有成功。该代码将在ANSYS Fluent中实现,以读取多个案例文件:
(define j 5)
(Do ((i 10 (+ i 1))) ((>= i 20))
(ti-menu-load-string (format #f "/file/read-case "C:/DataProcessing/Case~a-time~a-sec/test/Case~a-time~a-sec.cas"" i j i j))
(set! j (+ j 1))
)
如何将新的j
值传递给do-loop,以便我将文件夹和文件名称为更改如下:
Case10-time5-sec
Case11-time6-sec
...
我知道(set! j (+ j 1))
不是做事的正确方法,而是让您了解我要做的事情。我认为当变量更改值时不难调用该变量?
在列表中带有vars您只需添加一个nother术语:
(do ((i 10 (+ i 1))
(j 5 (+ j 1)))
((>= i 20) 'my-return-value)
(ti-menu-load-string
(format #f "/file/read-case "C:/DataProcessing/Case~a-time~a-sec/test/Case~a-time~a-sec.cas"" i j i j)))
; ==> my-return-value (and as side effect prints some strings)
知道do
只是递归功能的语法糖。您可以使用命名let
这样的情况而没有这样做:
(let function-name ((i 10) (j 5))
(if (>= i 20)
'my-return-value
(begin
(ti-menu-load-string
(format #f "/file/read-case "C:/DataProcessing/Case~a-time~a-sec/test/Case~a-time~a-sec.cas"" i j i j))
(function-name (+ i 1) (+ j 1)))))
实际上,您可以通过分隔生产和打印来使其发挥作用:
(define (make-strings)
(let function-name ((i 10) (j 5) (result '()))
(if (>= i 20)
(reverse result)
(function-name
(+ i 1)
(+ j 1)
(cons (format #f "/file/read-case "C:/DataProcessing/Case~a-time~a-sec/test/Case~a-time~a-sec.cas"" i j i j)
result)))))
(for-each ti-menu-load-string (make-strings))
关于此事的好处是,您可以单位测试make-strings
,将其扩展到输入变量等。