0

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?

Emma
  • 149
  • 10

1 Answers1

2

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..

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Thank you! that works very well. Can you please check the update that I have added? I thought that the problem can be solved if it's divided into pieces. I hope I have not created more confusion in the post! – Emma Dec 17 '17 at 09:38
  • @Emma they are unrelated problems so extending this question about variables in `do`loops is not a great idea. You add numbers with `+`. eg. `(+ 6 1/10); ==> 6.1`. I feel [I've answered this before](https://stackoverflow.com/q/47822902/1565698). Think about the next person who wants help with more than one variable. Is your edit useful for them or just confusing? I would have reverted and created a new querstion if you need. – Sylwester Dec 17 '17 at 13:03
  • ! The method you suggest `(+ 6 1/10); ==> 6.1` works for reading one case (one outer do-loop), but in the second loop it fails: In the first loop: `i` will be `10 `, `j=5`, `datafilenum` will change from: `5.100 =>6.000`, and in the second loop: `i =11`, `j =6` and `datafilenum` will change from `6.100 =>7.000` I don't know how to make `datafilenum` as a variable with 3 digits after the decimal point. As you can see, what is needed is to concatenate that variable `j` with the digits after the decimal point. Hope this clarifies the issue. – Emma Dec 17 '17 at 17:02
  • Stackoverflow does not recommend deleting questions as this may result in blocking one's account from asking new questions! – Emma Dec 17 '17 at 17:08
  • @Emma I suggested you reverted the changes back to keeping it to the subject. You can use `~0,3F` and it will zero pad on right side as I showed in [a different answer.](https://stackoverflow.com/q/47822902/1565698) – Sylwester Dec 17 '17 at 17:50
  • I have reverted the changes to this question and posted a new question [https://stackoverflow.com/q/47860246/5682472] with the updates, since I still didn't manage to get the code to work as required! – Emma Dec 17 '17 at 22:57