PasteRack.org
Paste # 2077
2017-04-21 06:46:53

Fork as a new paste.

Paste viewed 75 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 (count-such-that predicate collection)
  (for/sum ([x (in-set collection)]
            #:when (predicate x))
           1))

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

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

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

(define (card-suit  c) (vector-ref c 0))
(define (card-value c) (vector-ref c 1))
(define hand-size 5)
(define suits '(S H D C))
(define ranks '(A 2 3 4 5 6 7 8 9 T J Q K))
(define deck  (cross suits ranks))
(define Hands (combinations deck hand-size))

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

(time (P flush Hands))