This file contains 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
SET A, 0 | |
SET B, 1 | |
SET C, 10 ;; set to 10 to find the tenth fibonacci number | |
:test IFE C, 0 | |
SET PC, done | |
;; loop | |
SET X, A | |
ADD X, B |
This file contains 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 chop-1 (num tree) | |
(labels ((recur (num tree start-index) | |
(if (equalp 0 (length tree)) -1 | |
(let* ((mid-point (truncate (/ (length tree) 2))) | |
(mid-num (elt tree mid-point))) | |
(cond | |
((= (length tree) 0) -1) | |
((= mid-num num) (+ mid-point start-index)) | |
((= (length tree) 1) -1) | |
((> mid-num num) (recur num (subseq tree 0 mid-point) 0)) ;; check in first half of tree |
This file contains 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
# process() kind of parses Tadoku entries | |
# Character Classes | |
# 0. Space | |
# 1. Hash | |
# 2. Semicolon | |
# 3. Number | |
# 4. Other | |
This file contains 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 contains (self value) | |
(labels ((recur (value current) | |
(if (null current) | |
nil | |
(cond | |
((= value current.value) t) | |
((< value current.value) (recur value current.left)) | |
(t (recur value current.right)))))) | |
(recur value self.root))) |
This file contains 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
;; 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 |
This file contains 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
(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)) |
This file contains 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
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 |
This file contains 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
;;; 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 |
This file contains 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
// 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); |
This file contains 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
(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)))) |
OlderNewer