Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

combis.rkt

By: a guest on Sep 15th, 2012  |  syntax: Scheme  |  size: 1.70 KB  |  views: 50  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env racket
  2. #lang racket/base
  3.  
  4. ; --- callback version (fast)
  5. (define (combis_a stcs lpars callback)
  6.   (let ((res '()))
  7.     (let loop ((stcs (reverse stcs)) (subres '()))
  8.       (if (null? stcs)
  9.           (begin
  10.             (set! res (cons subres res)) ; ditto
  11.             (callback subres))
  12.           (for-each
  13.            (lambda (lpar) (loop (cdr stcs) (cons (cons (car stcs) lpar) subres)))
  14.            lpars)))
  15.     res))
  16.  
  17. ; --- generator version (slow)
  18. (require racket/generator)
  19. (define (combis_b_gen stcs lpars)
  20.   (generator ()
  21.     (let loop ((stcs (reverse stcs)) (res '()))
  22.       (if (null? stcs)
  23.           (yield res)
  24.           (for-each
  25.            (lambda (lpar) (loop (cdr stcs) (cons (cons (car stcs) lpar) res)))
  26.            lpars)))
  27.     'done))
  28.  
  29. ; --- main
  30. (let* ((args (current-command-line-arguments))
  31.        (numstcs (string->number (vector-ref args 0)))
  32.        (stcs (build-list numstcs (lambda (i) (string-append "S_" (number->string i)))))
  33.        (numlpars (string->number (vector-ref args 1)))
  34.        (lpars (build-list numlpars (lambda (i) (string-append "L_" (number->string i)))))
  35.        (verbose (string=? (vector-ref args 2) "y"))
  36.        (algo (vector-ref args 3)))
  37.   (cond
  38.     ((string=? algo "a")
  39.      (let ((res (combis_a stcs lpars (lambda (i) (when verbose (printf "combis_a: ~v\n" i))))))
  40.        (printf "combis_a: total ~a\n" (length res))))
  41.     ((string=? algo "b")
  42.      (let ((res (for/list ((combi (in-producer (combis_b_gen stcs lpars) 'done)))
  43.                   (when verbose (printf "combis_b: ~v\n" combi)) combi)))
  44.        (printf "combis_b: total ~a\n" (length res))))
  45.     (else
  46.      (printf "unknown command line arguments ~a\n" args))))
clone this paste RAW Paste Data