Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Created January 23, 2026 02:18
Show Gist options
  • Select an option

  • Save kmicinski/dded73efb14fdf7efae6ae337879702a to your computer and use it in GitHub Desktop.

Select an option

Save kmicinski/dded73efb14fdf7efae6ae337879702a to your computer and use it in GitHub Desktop.
#lang racket
;; Notes, 1/22
;; f is a function from α → bool (i.e., a predicate)
;; l is list of αs
;; we return the number of elements in l such that
;; they satisfy the predicate f
;; (count even? '(0 1 2 3 4)) ;; => 3
#;(define (count f l)
(if (empty? l)
0 ;; no elements in the list l, thus 0 elements satisfy f
;; we know l isn't empty
(+ (if (f (first l) 0 1)) (count f (rest l)))))
;; if I've got a list
;; (take '(x0 x1 x2 x3 x4 x5 ...) 3) => '(x0 x1 x2)
;; I can take N elements, which is restricting the list
;; to the first N elements of the list
(define (take l n)
(if (equal? n 0)
'()
(cons (first l) (take (rest l) (- n 1)))))
#;(define (take l n)
(cond
[(equal? n 0) '()]
[(> n 0) (cons (first l) (take (rest l) (- n 1)))]))
#;(define (take l n) ;; this is bad style because (null? n) is always #f
(if (empty? l) ;; if n is a number
0
(take (first l) (- n 1))))
;; drops the first N elements of the list l
(define (drop l n)
(cond [(or (zero? n) (null? l)) l]
;; if we get past the first branch, n>0
[else (drop (cdr l) (- n 1))]))
;; quickanswer.harp-lab.com
;; Now: moving into solving project 1
(define ex-board '(X O X O X O X E E))
(define (board-size board) (sqrt (length board)))
(define (list-ref l i)
(if (equal? i 0)
(first l)
(list-ref (rest l) (- i 1))))
(define (get-cell board r c)
(list-ref board (+ c (* r (board-size board)))))
(get-cell ex-board 1 1)
;; You win the game when one of the rows, columns, or diagonals
;; has the property that every element is 'X or 'O
;; For now, let's assume that we just care about the rows
;; forall / andmap is a function that asks:
;; does everything in the list satisfy a predicate?
(define (andmap f l)
(if (empty? l)
#t
(and (f (first l)) (andmap f (rest l)))))
(define (ormap f l)
(if (empty? l)
#f
(or (f (first l)) (ormap f (rest l)))))
;; does player win the list, which is a row/column/diagonal
(define (list-winner? l player)
;; use count to see how many elements in l are equal? to player?
;; if that count is equal to the length of l, then it's a winner
(equal? (count (λ (p) (equal? p player)) l)
(length l)))
;; assume lists is a list of rows, columns, etc.
;; is it the case that one of those rows / columns / etc.
;; satisfies the property that all elements are equal? to player?
(define (some-winner? lists player)
(ormap (λ (l) (list-winner? l player)) lists))
(define (rows board)
;; calculuate the size of the board (sqrt (length board))
;; recursively: take that size of elements from the board
;; then keep going after dropping that size of elements from the board
(define N (board-size board))
(define (h board)
(if (empty? board)
'()
(cons (take board N) (h (drop board N)))))
(h board))
(define (winner? board)
;; a list of lists
(define b-rows (rows board))
;; a list of lists
(define b-cols '()) ; (columns board))
;; a list (a single diagonal)
(define l-diag '()) ; (left-diagonal board))
;; a list (a single diagonal)
(define r-diag '()) ; (right-diagonal board))
;; now I've got everything as a bunch of lists
(define all (append b-rows b-cols (list l-diag r-diag)))
(cond
[(some-winner? all 'X) 'X] ;; 'X wins
[(some-winner? all 'O) 'O] ;; 'O wins
[else #f]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment