;; adapted from larceny benchmark suite
(letrec ([nqueens
          (lambda (n)
            (letrec ([one-to
                      (lambda (n)
                        (let loop ((i n) (l '()))
                          (if (= i '0) l (loop (- i '1) (cons i l)))))]
                     [my-try
                      (lambda (x y z)
                        (if (null? x)
                            (if (null? y) '1 '0)
                            (+ (if (ok? (car x) '1 z)
                                   (my-try (append (cdr x) y) '() (cons (car x) z))
                                   '0)
                               (my-try (cdr x) (cons (car x) y) z))))]
                     [ok?
                      (lambda (row dist placed)
                        (if (null? placed)
                            '#t
                            (and (not (= (car placed) (+ row dist)))
                                 (not (= (car placed) (- row dist)))
                                 (ok? row (+ dist '1) (cdr placed)))))])
              (my-try (one-to n) '() '())))])
    (nqueens '7))
