Skip to content

Instantly share code, notes, and snippets.

@ehaliewicz
ehaliewicz / calendar.go
Created March 31, 2021 18:27
calendar functions
func isLeap(y int) bool {
if y%4 == 0 {
if y%100 == 0 {
return y%400 == 0
}
return true
}
return false
}
@ehaliewicz
ehaliewicz / l.c
Created May 30, 2019 03:46
lambda
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
SUCCESS, // parsed
FAILURE, // didn't parse
ERROR // bad syntax
} status;
@ehaliewicz
ehaliewicz / 8queens.c
Created October 24, 2018 21:53
8queens
#include <stdio.h>
#include <stdint.h>
int columns[8] = {-1,-1,-1,-1,-1,-1,-1,-1};
int num_solutions = 0;
void print_queens() {
for(int y = 0; y < 8; y++) {
for(int x = 0; x < 8; x++) {
putchar(columns[x] == y ? 'Q' : '.');
@ehaliewicz
ehaliewicz / bf.lisp
Created July 30, 2018 17:37
brainfuck compiler to low-level lisp code
(defun compile-bf (program)
(let ((loop-stack (list)))
(let ((translated (loop for char across program appending
(case char
(#\> '((incf pointer)))
(#\< '((decf pointer)))
(#\+ '((incf (aref memory (wrap-pointer pointer)))))
(#\- '((decf (aref memory (wrap-pointer pointer)))))
(#\. '((format t "~a" (code-char (aref memory (wrap-pointer pointer))))))
(#\, '((setf (aref memory (wrap-pointer pointer)) (char-code (read-char)))))
@ehaliewicz
ehaliewicz / lambda-comp.c
Created May 31, 2017 18:53
lambda calculus compiler in 511 lines of C
// compile lambda calculus in a few lines (^:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <setjmp.h>
// syntax tree structures and utility functions
@ehaliewicz
ehaliewicz / lambda.c
Created May 26, 2017 17:48
lambda calculus in 255 lines of C
// interpret lambda calculus in a few lines (^:
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#include "setjmp.h"
typedef enum {
APPLICATION, LAMBDA, SYMBOL
} type;
#include "stdio.h"
#include "stdlib.h"
typedef struct {
void* apply_func; // pointer to function apply this closure w/ correct number of args and free-vars
void* func; // pointer to lifted function that implements this closure
int num_freevars; // number of bound variables (for GC use)
char* str; // string representation of this function
void* forwarded; // forward pointer (for GC use)
void* args[]; // bound variables
@ehaliewicz
ehaliewicz / lc->c.lisp
Last active August 29, 2015 14:00
lambda calculus -> c compiler
;; compiles untyped lambda calculus to portable C
;; syntax
;; (lambda x x) - lambda abstraction
;; (x y) - lambda combination (assuming x and y are bound)
;; map of function names to declarations
;; kept separate from the rest of the C code because they need to be forward-declared
(defvar *lambda-map* nil)
@ehaliewicz
ehaliewicz / lambda.lisp
Last active August 29, 2015 13:57
Lambda calculus interpreter in common lisp
(defun interpret-lambda (expr &optional env)
(etypecase expr
(symbol (let ((res (assoc expr env))) (if res (cdr res) (error "Unbound symbol"))))
(list (case (car expr)
(lambda (list (car (second expr)) (third expr) env))
(otherwise
(let ((rand (interpret-lambda (second expr) env))
(rator (interpret-lambda (first expr) env)))
(interpret-lambda
(cadr rator)
@ehaliewicz
ehaliewicz / fast-gol.lisp
Last active January 2, 2016 07:59
A quick game of life algorithm.
(ql:quickload "lispbuilder-sdl")
(ql:quickload "lispbuilder-sdl-gfx")
(deftype triplet () '(unsigned-byte 16))
(defmacro pixel-to-cell (val) `(/ ,val *cell-size*))
(defmacro cell-to-col (val) `(floor ,val 3))
(defmacro pixel-to-col (val) `(cell-to-col (pixel-to-cell ,val)))