Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

what is the best language to do this in?

Name: Anonymous 2010-01-14 19:13

I have a set of data:
Sally has attribute A and attribute B
Bob has attribute D and attribute R
John has attribute S and attribute T
Melissa has attribute G and attribute M

etc.  There are over 300 names and 21 different attributes (let's call them A, B, ..., T, U) in varying combinations.

I want to pick out a set of 5 names with 2 attributes each that fit in a pattern of (for example) A,B,C,D,E,F,H,I,J,K

What's the best way to go about this?  I'm programming language neutral, though I do have some experience in bash, python and perl.  It would have to take input from a text file (either tab delimited or csv) and output which names to pick and what their attributes are.

Name: Anonymous 2010-01-15 11:41

Here's a collect macro using amb that is not terribly robust or scalable.

(define-syntax collect
  (syntax-rules ()
    ((_ ((a b ...) c ...) e ...)
     (let ((collection empty))
       (let ((*failure* (λ() (reverse collection))))
         (letrec-syntax ((amb (syntax-rules ()
                                ((amb) (*failure*))
                                ((amb ?x) ?x)
                                ((amb ?x ?y)
                                 (let ((old-failure *failure*))
                                   ((call-with-current-continuation
                                     (lambda (cc)
                                       (set! *failure*
                                             (lambda ()
                                               (set! *failure* old-failure)
                                               (cc (lambda () ?y))))
                                       (lambda () ?x))))))
                                ((amb ?x ?rest (... ...))
                                 (amb ?x (amb ?rest (... ...))))))
                         (ambify (syntax-rules ()
                                   ((_ ((?a ?b (... ...)) ?c (... ...)) ?proc ?arg (... ...))
                                    (let ((?a (amb ?b (... ...))))
                                      (ambify (?c (... ...)) ?proc ?a ?arg (... ...))))
                                   ((_ () ?proc ?arg (... ...)) (let ((result (?proc ?arg (... ...))))
                                                                  (if result
                                                                      (if (eq? result
                                                                               (if (empty? collection)
                                                                                   empty
                                                                                   (first collection)))
                                                                          (amb)
                                                                          (set! collection (cons result collection)))
                                                                      (amb))
                                                                  (amb))))))
           (ambify ((a b ...) c ...) e ...)))))))

(collect ((hot 'harry 'draco 'hermione)
          (wizard 'harry 'draco 'hermione 'ron 'dumbledore)
          (male 'harry 'draco 'ron 'dumbledore)
          (female 'hermione)
          (nice-hair 'hermione 'draco))
         (λ traits
           (let ((hot (first traits))
                 (nice-hair (last traits)))
             (if (eq? hot nice-hair)
                 hot
                 #f))))
; => (draco hermione)

(let ((dice-rolls (collect ((die1 1 2 3 4 5 6)
                            (die2 1 2 3 4 5 6)
                            (die3 1 2 3 4 5 6))
                           (λ dice dice))))
  dice-rolls)
 ; => ((1 1 1)
 ;     (2 1 1) ...) all the ways to roll three dice

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List