chicken-users
[Top][All Lists]

## Re: [Chicken-users] nested loop over lists

 From: Jinsong Liang Subject: Re: [Chicken-users] nested loop over lists Date: Fri, 15 Jul 2016 21:48:49 -0400

Thank you Josh! I learned "unless" from your code to replace my (when (not ...)).

Jinsong

On Fri, Jul 15, 2016 at 7:29 PM, Josh Barrett wrote:
You can also use recursion:
(let l1 ((i '(1 2 3)))
(let l2 ((j '(4 5 6)))
(let l3 ((k '(7 8 9)))
(print (+ (car i) (car j) (car k)))
(unless (null? k) (l3 (cdr k))))
(unless (null? j) (l2 (cdr j))))
(unless (null? i) (l1 (cdr i)))

This is generally considered bad style, and insane. Instead, you can use Scheme's native iteration construct, do:

(do ((i 1 (+ i 1)))
((<= i 3) #f)
(do ((j 4 (+ j 1)))
((<= j 6) #f)
(do ((k 7 (+ k 1)))
((<= k 9) #f)
(print (+ i j k)))))

This is still bad style, but it's not totally insane. Map, which takes the same arg format as for-each, is a better alternative, as are all the previous replies. The advantage of these awful approaces, and map, is that the will work on 90% of the blind, flea-bitten, half-implemented scheme implementations you come across/write yourself. And there are quite a few. So, even of you never use these functions, it's good to know they exist.

On Thu, Jul 14, 2016, 14:11 Jinsong Liang <address@hidden> wrote:
Hi Kevin,

Thank you for your suggestions! It is good to know the "big 3".  I will read the SRFI 1 documentation.

Jinsong

On Thu, Jul 14, 2016 at 1:55 PM, Kevin Wortman wrote:

Another perspective, if you're new to Scheme, I'd recommend getting in the habit of using higher-order procedures before loops. The principle is that looping is an error-prone (e.g. infinite loops) low level operation, so it's best to factor that out and use an established and tested library procedure instead of writing new iteration logic each time.

The big 3 are filter, fold, and map in SRFI 1 ( http://wiki.call-cc.org/man/4/Unit%20srfi-1 ).

I'd actually implement your pseudocode with

(for-each (lambda (i)
(for-each (lambda (j)
(for-each (lambda (k)
(print (+ i j k)))
'(7 8 9)))
'(4 5 6)))
'(1 2 3))

You mentioned "calculation" so I wager you'll actually want to use fold, not for-each.

Regards,
Kevin Wortman

On Thu, Jul 14, 2016 at 7:04 AM Jinsong Liang <address@hidden> wrote:
Wow, this is amazing! Thanks a lot Christian!

Jinsong

On Thu, Jul 14, 2016 at 3:05 AM, Christian Kellermann wrote:
* Jinsong Liang <address@hidden> [160714 04:26]:
> Hi,
>
> I want to do nested loops over three lists like the following pseudo code:
>
> for i in '(1 2 3)
>     for j in '(4 5 6)
>         for k in '(7 8 9)
>           //do calculation using i, j, and k. The three lists are not
> related.
>         end
>     end
> end
>
> What is the best way to do this in Chicken? I can use (for-each ...) or (do
> ...) but it seems neither is straightforward.

Without knowing the purpose of this it's hard to say.  However scheme
is flexible enough to allow an almost verbatim translation using
foof-loop:

(use foof-loop)

(loop ((for i (in-list '(1 2 3))))
(loop ((for j (in-list '(4 5 6))))
(loop ((for k (in-list '(7 8 9))))
(print "Magic " i "+" j "+" k " = " (+ i j k)))))

HTH,

Christian

--
May you be peaceful, may you live in safety, may you be free from
suffering, and may you live with ease.

_______________________________________________
Chicken-users mailing list