Skip to content

Instantly share code, notes, and snippets.

View no-defun-allowed's full-sized avatar

Hayley Patton no-defun-allowed

View GitHub Profile
@no-defun-allowed
no-defun-allowed / install-sicl.sh
Last active December 29, 2020 06:18
A script to download the sources of SICL and its dependencies
#!/bin/bash
PROJECTS=${1:-~/quicklisp/local-projects}
clone() {
TARGET=$PROJECTS/$(basename $1)
if [[ -d $TARGET ]]; then
(cd $TARGET; git pull)
else
git clone $1 $TARGET
@no-defun-allowed
no-defun-allowed / trading-simulation.lisp
Created December 28, 2020 22:18
someone whose only qualifications are getting 90% on a statistics test in high school investigates the Dream speedrun controversy
(declaim (optimize (speed 3) (safety 1)))
(defconstant +pearl-probability+ (float 20/423))
(declaim (inline trade))
(defun trade ()
(if (< (random 1.0) +pearl-probability+)
;; Note that RANDOM returns a value in [0, maximum), i.e.
;; it will only produce numbers below the maximum given.
(+ 4 (random 5))
0))
(ql:quickload :the-cost-of-nothing)
(in-package :cl-user)
(defun foo (x)
(declare (integer x)
(optimize (speed 3)))
(< (- (expt 2 256)) x (expt 2 256)))
(the-cost-of-nothing:bench (foo 7))
(in-package :sb-c)
@no-defun-allowed
no-defun-allowed / subtypep-set-arithmetic.lisp
Last active July 11, 2020 01:28
Asking logic questions using SUBTYPEP
CL-USER> (defun true? (p)
"Is P the universal set, i.e. is our statement P true?"
(subtypep 't p))
TRUE?
CL-USER> (deftype implies (p q)
"Are all elements of P elements of Q? (This is basically SUBTYPEP again.)"
`(or (not ,p) ,q))
IMPLIES
CL-USER> (true? '(implies cons list)) ; Does CONS imply LIST?
;; Note that SUBTYPEP returns:
@no-defun-allowed
no-defun-allowed / regexp.hs
Last active April 22, 2020 01:36
Stupid regular expression engine in Haskell
data NFAState = Match Char NFAState |
MatchAll NFAState |
Amb NFAState NFAState | Halt
deriving Show
data Automaton = Running [Char] NFAState | Finished deriving Show
finished :: Automaton -> Bool
finished (Running _ _) = False
finished Finished = True
@no-defun-allowed
no-defun-allowed / fft.lisp
Created March 22, 2020 09:58
The Fast Fourier Transform in Petalisp
(use-package :petalisp)
(defun fft (x)
(let ((n (shape-size (shape x))))
(assert (= (shape-rank (shape x)) 1))
(assert (zerop (logand n (1- n))) ()
"The length is not a power of 2")
(if (<= n 1)
x
(let* ((even (fft (reshape x (~ 0 2 (1- n))
@no-defun-allowed
no-defun-allowed / lir-generator-and-printer.lisp
Created December 29, 2019 09:30
A LIR printer and a utility function for making Cleavir HIR, MIR and LIR
(ql:quickload '(:concrete-syntax-tree :trucler-native
:cleavir2-cst-to-ast :sicl-ast-to-hir
:sicl-hir-to-mir :sicl-mir-to-lir))
(defvar *environment*)
(defvar *client*)
(defun expression-to-ir (expression &key (type :lir))
(let* ((cst (concrete-syntax-tree:cst-from-expression expression))
(ast (cleavir-cst-to-ast:cst-to-ast *client* cst *environment*))
(hir (sicl-ast-to-hir:ast-to-hir *client* ast)))
@no-defun-allowed
no-defun-allowed / nn3.lisp
Created April 19, 2019 02:21
A half-baked neural network library using Petalisp.
(ql:quickload :petalisp.examples)
(use-package '(:petalisp :petalisp.examples.linear-algebra))
;; we use this internal function in our hack over transpose and matmul.
(import 'petalisp.examples.linear-algebra::coerce-to-matrix)
(declaim (optimize (speed 3) (safety 1) (debug 3)))
;; i use these modified function definitions to produce the ranges
;; [0, n - 1] instead of [1, n], which makes handling them aside already
;; computed arrays much easier.
(defun transpose (x)
@no-defun-allowed
no-defun-allowed / nn2.lisp
Created March 17, 2019 07:24
A deeper neural network using Petalisp.
(ql:quickload :petalisp.examples)
(use-package '(:petalisp :petalisp.examples.linear-algebra))
;; We use this internal function in our hack over transpose and matmul.
(import 'petalisp.examples.linear-algebra::coerce-to-matrix)
(declaim (optimize (speed 3) (safety 1) (debug 0)))
;; I use these modified function definitions to produce the ranges
;; [0, n - 1] instead of [1, n], which makes handling them aside already
;; computed arrays much easier.
(defun transpose (x)
@no-defun-allowed
no-defun-allowed / parse.lisp
Created January 4, 2019 08:04
lisp parsing lisp
(defun read-list (stream)
(if (char= (peek-char t stream) #\))
(progn
(read-char stream)
nil)
(cons (read-sexp stream)
(read-list stream))))
(defun read-atom (stream &optional (collected nil))
(let ((char (peek-char (null collected) stream nil :eof)))