Skip to content

Instantly share code, notes, and snippets.

@sleibrock
Created December 31, 2021 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sleibrock/5337b9782dfb0e1bd917185dbffe74d1 to your computer and use it in GitHub Desktop.
Save sleibrock/5337b9782dfb0e1bd917185dbffe74d1 to your computer and use it in GitHub Desktop.
Betting.rkt
#lang racket/base
;; utilities
(struct Bet (name target monies) #:transparent)
(define *bets* (make-parameter '()))
(define *total* (make-parameter 0.0))
(define *wagers* (make-parameter (make-immutable-hash)))
(define (sum datums) (foldl + 0.0 datums))
(define (produce-wager-table bets)
(define total (sum (map Bet-monies bets)))
(define (reducer b hashy)
(let ([key (Bet-target b)]
[val (Bet-monies b)])
(if (hash-has-key? hashy key)
(hash-update hashy key (λ (x) (+ x val)))
(hash-set hashy key val))))
(foldl reducer (make-immutable-hash) bets))
;; user interface
(define (clear-bets)
(*total* 0.0)
(*wagers* (make-immutable-hash))
(*bets* '()))
(define (place-bet Name Target Monies)
(*total* (+ (*total*) Monies))
(*bets* (cons (Bet Name Target Monies) (*bets*)))
(*wagers* (produce-wager-table (*bets*))))
(define (bets)
(for-each displayln (*bets*))
(displayln (format "Pool: $~a" (*total*))))
(define (odds)
(for-each
(λ (key)
(displayln
(format "~a -> ~a" key
(/ (*total*) (hash-ref (*wagers*) key)))))
(hash-keys (*wagers*)))
(displayln (format "Total: ~a" (*total*))))
; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment