Skip to content

Instantly share code, notes, and snippets.

(defmacro alet (letargs &rest body)
`(let ((this) ,@letargs)
(setq this ,@(last body))
,@(butlast body)
(lambda (&rest params)
(apply this params))))
(defmacro alet-fsm (&rest states)
`(macrolet ((state (s)
`(progn (setq this #',s)
@ehaliewicz
ehaliewicz / vm-emulator.rkt
Created January 6, 2013 08:27
An emulator for a simple cpu.
(define registers (vector 0 0 0 0 0 0 0 0))
(define memory (make-vector 65536))
(define pc 0)
(define (load-program instructions)
(define (recur cnt rem)
(if (null? rem)
#t
@ehaliewicz
ehaliewicz / fib.rkt
Last active December 11, 2015 23:58
Lazy infinite fibonacci stream
(define fib
(lambda (n)
(((lambda (f)
((lambda (x) (x x))
(lambda (y) (f (lambda (a b)
((y y) a b))))))
(lambda (f)
(lambda (s n)
(if (= n 0)
((lambda (c) (c (lambda (a b) a))) s)
@ehaliewicz
ehaliewicz / infib.rkt
Last active December 11, 2015 23:59
Crazy stuff
;;;; lazy streams
(defmacro delay (expression)
`(lambda () ,expression))
;;; ` (backquote) means construct a list just like ' (quote)
;;; but it allows you to evaluate parts inside it with , (comma)
;;; also, the result of a macro is evaluated,
;;; so `(lambda () ,3) is evaluated as if it were just (lambda () 3)
# A small DSL for helping parsing documents using Nokogiri::XML::Reader. The
# XML Reader is a good way to move a cursor through a (large) XML document fast,
# but is not as cumbersome as writing a full SAX document handler. Read about
# it here: http://nokogiri.org/Nokogiri/XML/Reader.html
#
# Just pass the reader in this parser and specificy the nodes that you are interested
# in in a block. You can just parse every node or only look inside certain nodes.
#
# A small example:
#
@ehaliewicz
ehaliewicz / bf.lisp
Last active December 14, 2015 05:19
threaded brainfuck interpreter and simple compiler
(eval-when (:load-toplevel :execute :compile-toplevel)
(defun mkstr (&rest args)
(with-output-to-string (s)
(dolist (a args) (princ a s))))
(defun symb (&rest args)
(values (intern (apply #'mkstr args)))))
@ehaliewicz
ehaliewicz / repl.lisp
Last active December 14, 2015 10:49
lisp vm and compiler
;; compile-and-go creates a VM, compiles the given expression,
;; and runs the resulting assembly as the first expression in the vm REPL
(compile-and-go '(define (compiled-tail-recursive-fib n)
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
(fib-iter 1 0 n)))
@ehaliewicz
ehaliewicz / genlisp.lisp
Last active December 14, 2015 11:09
VM Repl interpreter and compiler
GENLISP> (gen-eval)
GEN-Eval: (+ 1 2)
;; total-stack-pushes: 6 maximum-stack-depth: 5
;; instructions executed: 86 execution time: 0.05 seconds
=> 3
GEN-Eval: (compile (+ 1 2))
;; total-stack-pushes: 0 maximum-stack-depth: 0
@ehaliewicz
ehaliewicz / compiler.lisp
Last active December 14, 2015 11:19
Compiler inlining example
;; 'val is the register where the return value of compiling goes
;; 'next is the requested type of compiler linkage, how the compiler will finish off the compiled code
;; 'return linkage returns to the state on top of the stack (to return from a function)
;; 'next linkage just continues onto whatever is the next instruction after the compiled instructions
;; any other linkage assumes a label,
;; i.e. (compile '(+ 1 2 3) 'val 'end) assumes 'end is a label somewhere,and jumps to it after calculating (+ 1 2 3)
(statements (ec-compile '(+ 1 2 3) 'val 'next)) ;; compile without inlining/open-coding
=>
@ehaliewicz
ehaliewicz / defmemo.lisp
Created March 12, 2013 01:50
automatic memoization macro
(defun fib (x)
(if (< x 2) x (+ (fib (- x 2))
(fib (- x 1)))))
(time (fib 10))
=> 1x10^4 cycles
(time (fib 20))
=> 1x10^6 cycles