guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

## loop translations (was: Re: language translator help)

 From: John W. Eaton Subject: loop translations (was: Re: language translator help) Date: Sat, 27 Apr 2002 19:53:33 -0500

```On 27-Apr-2002, Neil Jerram <address@hidden> wrote:

| I'm not aware of such a document.  Suggest you post the translation
| code that you've tried already.  I'll certainly take a look, and I
| guess you'll get a deluge of suggestions from others as well.

OK, here goes.

I'd like to understand how to translate some simple loops.  For
example, the following loop is valid in Octave.

for i = 1:3
disp (i);
if (i == 2)
break;
endif
endfor

This should print

1
2

Note that this sequence of statements doesn't return any value.  For
Guile, the best I've come up with is

(let for ((i 1))
(call-with-current-continuation
(lambda (break)
(if (<= i 5)
(begin
(display i)
(newline)
(if (= i 2)
(break (if #f #f)))
(for (+ 1 i)))))))

This works, but seems quite complex to me.  Is there a simpler way?

Here is another:

for i = 1:4
if (i < 4)
disp ("foo-i-hithere");
if (i == 2)
continue;
endif
endif
disp (i);
endfor

This should print

foo-i-hithere
1
foo-i-hithere
foo-i-hithere
3
4

For Guile, I came up with

(let for ((i 1))
(call-with-current-continuation
(lambda (continue)
(if (<= i 4)
(begin
(if (< i 4)
(begin
(display "foo-i-hithere")
(newline)
(if (= i 2)
(continue (for (+ 1 i))))))
(display i)
(newline)
(for (+ 1 i)))))))

Again, this works but is there a better way?  It seems undesirable to
duplicate the (for (+ 1 i)) bit.

jwe

```

reply via email to

 [Prev in Thread] Current Thread [Next in Thread]