PasteRack.org
Paste # 1405
2017-04-21 05:52:09

Fork as a new paste.

Paste viewed 90 times.


Embed:

#lang racket/base
(require racket/set racket/list)

(define predicate? procedure?)

(define (such-that predicate collection)
  (for/set ([x (in-set collection)]
            #:when (predicate x))
    x))

(define (to-set x)
  (if (set? x) x (list->set x)))

(define (P event space)
  (let* ([space (to-set space)]
         [event (if (predicate? event)
                    (such-that event space)
                    (to-set event))])
    (/ (set-count (set-intersect event space))
       (set-count space))))

(define (cross A B)
  (for*/list ([a (in-list A)]
              [b (in-list B)])
    (list a b)))

(define (combos items n)
  (for/set ([combo (in-combinations items n)])
    (append* combo)))

(define suits '(S H D C))
(define ranks '(A 2 3 4 5 6 7 8 9 T J Q K))
(define deck  (cross ranks suits))
(define Hands (combos deck 5))

(define (flush hand)
  (for/or ([suit (in-list suits)])
    (= (count (λ (h) (eq? h suit)) hand) 5)))

(time (P flush Hands))