This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun while (fn seq) | |
| "Evaluates to the sequence that results from calling fn on each element of seq, until the result is NIL" | |
| (labels ((aux (seq acc) | |
| (if (null seq) acc | |
| (let ((val (funcall fn (first seq)))) | |
| (if val | |
| (aux (rest seq) (cons val acc)) | |
| acc))))) | |
| (aux seq nil))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require 'thingatpt) | |
| (defun beginning-of-integer-at-point () | |
| (let ((inhibit-changing-match-data t)) | |
| (skip-chars-backward "[[:digit:]]") | |
| (unless (looking-at "[[:digit:]]") | |
| (error "No integer here")) | |
| (when (looking-back "[+-]") | |
| (backward-char 1)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define-modify-macro updatef (f &rest args) | |
| (lambda (r f &rest args) (apply f (cons r args))) | |
| "'Update' the value at a place by applying function F to the old value and any supplied arguments.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun find-first-unique (s) | |
| "return the first (leftmost) unique character in string s, or nil if | |
| none found" | |
| (let ((freqs (make-hash-table))) | |
| (loop for c across s do (incf (gethash c freqs 0))) | |
| (find-if (lambda (x) (= 1 (gethash x freqs))) s))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun find-first-unique (s) | |
| "return the first (leftmost) unrepeated character in string s or nil if none found" | |
| (labels ((aux (s uniq rep) | |
| (if (null s) (car (last uniq)) | |
| (destructuring-bind (f &rest r) s | |
| (cond | |
| ((member f rep) | |
| (aux r uniq rep)) | |
| ((member f uniq) | |
| (aux r (remove f uniq) (cons f rep))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defvar ticket-price 2) | |
| (defvar fixed-payouts (list 1000000 50000 100 100 7 7 4 4 0)) | |
| (defun 1-in (x) | |
| (/ 1 x)) | |
| (defvar probabilities | |
| (map 'list #'1-in | |
| (list 282201388.0 | |
| 11688053.52 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defparameter *outcomes* '(terrible poor mediocre fair good great superb)) | |
| (defun build-outcome-alist () | |
| (loop for n from 0 for o in *outcomes* collect (cons n o))) | |
| (defparameter *outcome-table* (build-outcome-alist)) | |
| (defparameter *superb-value* (car (rassoc 'superb *outcome-table*))) | |
| (defparameter *terrible-value* (car (rassoc 'terrible *outcome-table*))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require :split-sequence) | |
| (use-package :split-sequence) | |
| (defun bits->text (s) | |
| "\"01100110 01101111 01101111\" -> \"foo\"" | |
| (coerce | |
| (mapcar (lambda (x) (code-char (parse-integer x :radix 2))) | |
| (split-sequence #\space s)) | |
| 'string)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun hash-table-unzip (table) | |
| (with-hash-table-iterator (iter table) | |
| (labels ((aux (keys values) | |
| (multiple-value-bind (more? k v) (iter) | |
| (if more? | |
| (aux (cons k keys) (cons v values)) | |
| (values keys values))))) | |
| (aux nil nil)))) | |
| (defun hash-table-keys (table) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun pros (head tail) | |
| (lambda (m) (funcall m head tail))) | |
| (defun kar (l) | |
| (funcall l (lambda (head _) (declare (ignore _)) head))) | |
| (defun kdr (l) | |
| (funcall l (lambda (_ tail) (declare (ignore _)) tail))) | |
| (defun mth (l m) |