Skip to content

Instantly share code, notes, and snippets.

View baioc's full-sized avatar
🌑
Does the Black Moon howl?

Gabriel B. Sant'Anna baioc

🌑
Does the Black Moon howl?
View GitHub Profile
@baioc
baioc / Ahmose.scm
Last active October 4, 2019 12:19
Russian Peasant Multiplication Scheme
;; Egyptian / Russian peasant multiplication in O(lg(n))
(define (times b n)
(define (iter b n sum)
(cond ((= n 0) sum)
((even? n) (iter (double b) (halve n) sum))
(else (iter b (- n 1) (+ b sum)))))
(if (< n 0)
(iter (- b) (- n) 0)
(iter b n 0)))
@baioc
baioc / Fibonacci.m
Last active October 16, 2019 22:34
Closed-Form Fibonacci through Linear Algebra
% Octave code showing how to compute Fibonacci sequence numbers through Linear
% Algebra concepts (*eigen*-stuff) applied in order to reach efficient O(lg(n))
% complexity (supposing exponentiation is O(lg(n))). This method also correctly
% calculates the "negafibonacci" sequence, that is, Fibonacci(n)` when n is negative.
% For big values of n, however, the floating-point operations yield +infinity.
function fn = Fibonacci(n)
% golden ratio number and its conjugate are eigen-values
phi = (1 + sqrt(5)) / 2;
@baioc
baioc / memoize.scm
Last active February 19, 2021 09:59
Automatic Memoization with Closures
;; make a function that caches its past results
(define (memoize proc)
(let ((cache (make-table)))
(define (delegate . args)
(let ((hit (lookup cache args)))
(or hit ; or miss
(let ((result (apply proc args)))
(insert! cache args result)
result))))
delegate))
@baioc
baioc / atomic.asm
Last active September 30, 2022 14:56
Spin-Lock Mutex in MIPS-32
.text
# void lock(mutex)
lock:
ll $t0, 0($a0) # read the mutex (and set LLbit)
bne $t0, $zero, lock # if occupied (1), start over
addi $t1, $zero, 1
sc $t1, 0($a0) # try grabing the mutex
beq $t1, $zero, lock # if wasn't atomic (0), start over
@baioc
baioc / chkxml.cpp
Last active October 16, 2019 22:33
XML tag-balance-checking algorithm
/*
* See the full version at <https://gitlab.com/snippets/1849449>
* and a pure C implementation at <https://gitlab.com/snippets/1837083>
*/
#include <stack>
#include <string>
bool balanced(const std::string& xml)
@baioc
baioc / unique_ptr.hpp
Last active September 14, 2019 21:10
A simple smart pointer
#ifndef UTIL_UNIQUE_PTR_HPP
#define UTIL_UNIQUE_PTR_HPP
#include <utility>
namespace util {
template <typename T>
class unique_ptr {
public:
@baioc
baioc / fibonacci.scm
Last active November 7, 2019 01:57
Recursive Fibonacci Schemes
;;; Gabriel B. Sant'Anna <baiocchi.gabriel@gmail.com>
;;; Explained in pt-br at <https://baioc.github.io/scheme/>
;; binary-recursive aka dumb fibonacci
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
@baioc
baioc / Combinatorial.hs
Last active December 3, 2019 21:18
Lazy Permutations in Haskell
module Combinatorial (
combine,
permutations,
matrices,
) where
-- inserts a value in a list at every possible position
combine :: t -> [t] -> [[t]]
combine x [] = [[x]]
@baioc
baioc / hornify.scm
Created February 19, 2021 09:55
Polynomial definition and conversion to Horner form
(import (scheme base))
(define-syntax hornify
(syntax-rules ()
((_ x an) an)
((_ x a pn ...)
(+ a (* x (hornify x pn ...))))))
(define-syntax poly
(syntax-rules ()
@baioc
baioc / gyre_example.d
Created July 6, 2022 16:28
GyreD API example
import gyre.mnemonics;
Graph graph;
graph.initialize(MacroNode.ID(42));
scope(exit) graph.dispose();
assert(graph.exported == 0);
assert(graph.length == 0);
// imagine we're compiling something like