Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / 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
(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 / thread.lisp
Last active October 14, 2015 00:58
a simple function threading macro
(defmacro -> (init &rest funcs)
(cond
((null funcs) `,init)
((eql 'if (car funcs))
(let* ((gensym (gensym)))
`(-> (let ((,gensym ,init))
(if ,gensym (-> ,gensym ,(cadr funcs))
(-> ,gensym ,(caddr funcs)))
,@(cdddr funcs)))))
((and (consp (car funcs)) (not (eql 'lambda (caar funcs))))
@ehaliewicz
ehaliewicz / flood_fill.c
Created November 28, 2012 23:54
Random flood fill
// create stack and other structures
stack_t* draw_stack = (stack_t*)malloc(sizeof(stack_t));
stack_init(draw_stack, (wa.width*wa.height));
stack_element_t random_start;
// push random position onto stack
random_start.x = rand()%wa.width;
random_start.y = rand()%wa.height;
stack_push(draw_stack, random_start);
@ehaliewicz
ehaliewicz / flood.lisp
Created November 28, 2012 01:43
Random flood fill
;;; recur is like clojure's loop/recur
;;;
;;; declare a tail-recursive loop with
;;; (recur ( (var-name (&optional type) init-val) ... more vars))
;;;
;;; recur back to the beginning with
;;; (tail-recur (var-name new-value) ... more vars)
;;; (exit &optional val)
;;; leaves the recursion returning either nil, or the given value
class Class
def attr_accessor_with_default(attr_name, val)
attr_name = attr_name.to_s
self.class_eval("def #{attr_name}; @#{attr_name} = #{val};end")
self.class_eval("@#{attr_name} = #{val}")
self.class_eval("def #{attr_name}=(val) @#{attr_name} = val end")
end
@ehaliewicz
ehaliewicz / flip-flop.lisp
Last active October 12, 2015 08:27
A flip-flop operator for lisp
(defmacro flip-flop (left right)
"Evaluates to false until the left test is satisfied. Then evaluates to true until the right test is satisfied, etc."
;; create a collision-safe name for our state variable
(let ((state-var (gensym)))
;; create a toplevel variable to keep state of flip-flop
`(progn
(defvar ,state-var nil))
@ehaliewicz
ehaliewicz / tail-recur.lisp
Created October 28, 2012 00:49
Tail-recursion and mutual recursion macros
;; A couple of silly macros for a language that has defmacro but no tail-recursion
;; the mutual recursion macro is even worse, because you can only really 'call' one of the functions
(defmacro recur (arg-pairs &body body)
(let ((arg-names (mapcar #'car arg-pairs)) ;; extract loop variable names
(arg-vals (mapcar #'cadr arg-pairs))) ;; extract start values
;; block to return from
`(block nil