Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

Nested loops over lists


I'm a really big fan of Lisp, and particularly Scheme.

It's a relatively short time that I've been studying the language. I'm
really enjoying the SICP videos. I'm a math student, and I wanted to
produce a table of boolean values for various operations such as AND,
OR, etc...

Like this:

AND table:

A     B     Result
------------------------
#f     #f     #f
#f     #t     #f
#t     #f     #f
#t     #t     #t

I know Scheme hasn't got Common Lisp's loop special form so I tries to
figure a way of doing it, and I cooked up the following two solutions.
Can anybody tell me if they're acceptable enough or whether there are
better ways of iterating over lists especially in NESTED loops?

Here's the code:

(define boolean (list #f #t))

(let loop ((list-a boolean) (list-b boolean))
  (if (null? list-a)
      'done
      (if (null? list-b)
          (loop (cdr list-a) boolean)
          (let ((a (car list-a)) (b (car list-b)))
            (display a)
            (display " ")
            (display b)
            (display " ")
            (display (or a b))
            (newline)
            (loop list-a (cdr list-b))))))

Yet another with for-each:

(for-each (lambda (a)
            (for-each (lambda (b)
                        (begin
                          (display a)
                          (display " ")
                          (display b)
                          (display " ")
                          (display (or a b))
                          (newline)))
                      '(#f #t)))
          '(#f #t))

Thanks for your help.

rocco.ro@gmail.com writes:

...

This one is all right. The other one essentially implemented a special
case of for-each, for no good reason.

If you do this more than once, abstract the pattern in a procedure:

(define (for-each-pair proc as bs)
   (for-each (lambda (a)
                (for-each (lambda (b)
                             (proc a b))
                          bs)
             as)))

(define (or-line a b)
   (for-each display (list a " " b " " (or a b)))
   (newline))

(for-each-pair or-line '(#f #t) '(#f #t))

If you do it once, write as you wrote.

rocco.ro@gmail.com skrev:

There's alway do loops:

 > (do ([as boolean (cdr as)])
     [(null? as) 'done]
     (do ([bs boolean (cdr bs)])
         [(null? bs) 'done]
         (let ([a (car as)]
               [b (car bs)])
           (display (format "~a ~a ~a\n" a b (or a b))))))
#f #f #f
#f #t #t
#t #f #t
#t #t #t
done

But srfi-42 is also an option:

 > (require (lib "42.ss" "srfi"))
 > (do-ec (: a boolean)
          (: b boolean)
          (display (format "~a ~a ~a\n" a b (or a b))))
#f #f #f
#f #t #t
#t #f #t
#t #t #t

Eager Comprehensions for Black Belts - An SRFI 42 Tutorial
http://www.scheme.dk/blog/2006/08/eager-comprehensions-for-black-belt...

--
Jens Axel Sgaard

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc