如何在Scheme语言中的do循环中增加计数器? [英] How to increase counter in a do-loop within Scheme language?

查看:612
本文介绍了如何在Scheme语言中的do循环中增加计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Scheme语言的do循环中做一个简单的计数器增加,但我不熟悉该语言,并尝试过许多脚本而没有成功。该代码将在Ansys Fluent中实现以读取多个案例文件:

 (define j 5)
( Do((i 10(+ i 1)))((> = i 20))
(ti-menu-load-string(格式#f/ file / read-case \C:/ DataProcessing / Case〜a-time〜a-sec / test / Case〜a-time〜a-sec.cas \ijij))
(set!j(+ j 1))

如何传递新的 j 值到do循环,以便我得到文件夹和文件名称如下所示:

  Case10-time5-sec 
Case11-time6-sec
...

我知道(set!j(+ j 1))不是正确的做事方式,而是让你知道我在做什么。我认为在改变值的时候调用这个变量不应该很难吗?

添加一个新术语:

 (do((i 10(+ i 1))
(j 5(+ j)1)))
((> = i 20)'my-return-value)
(ti-menu-load-string
(format #f/ file / case \C:/DataProcessing/Case~a-time~a-sec/test/Case~a-time~a-sec.cas\ijij)))
; ==> my-return-value(作为副作用打印一些字符串)

知道 do 就是递归函数的语法糖。您可以在没有它的情况下使用名称 let 来完成此操作:

  (让函数名((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 )
(function-name(+ i 1)(+ j 1)))))

实际上,你可以通过分割生产和打印来实现功能:

 (define make())
(let function-name((i 10)(j 5)(result'()))
(if(> = i 20)
(reverse result)
(函数名
(+ i 1)
(+ j 1)
(cons(格式#f/ file / read-case \C:/ DataProcessing /Case~a-time~a-sec/test/Case~a-time~a-sec.cas\ijij)
result)))))

(for -aach ti-menu-load-string(make-strings))

好的事情是你可以单元测试 make-strings ,并将其扩展采取输入变量等。

I want do a simple counter increase by one within a do-loop in Scheme language, but I'm not that familiar with the language and have tried many scripts without success. The code is going to be implemented in Ansys Fluent to read multiple case files:

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

How to pass the new j value to the do-loop so that I get the folder and file names to change as follows:

Case10-time5-sec
Case11-time6-sec
...

I know that the (set! j (+ j 1)) is not the correct way to do things, but to give you an idea of what I'm trying to do. I don't think it should be difficult to call the variable when it changed value?

解决方案

In the list with vars you just add a nother term:

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

Know that do is just syntax sugar for a recursive function. You could do this without it like this with a named 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)))))

Actually then you could make it functional by dividing producing and printing:

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

Nice thing about this is that you can unit test make-strings, extend it to take input variables etc..

这篇关于如何在Scheme语言中的do循环中增加计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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