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
;; 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))) |
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
% 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; |
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
;; 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)) |
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
.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 |
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
/* | |
* 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) |
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
#ifndef UTIL_UNIQUE_PTR_HPP | |
#define UTIL_UNIQUE_PTR_HPP | |
#include <utility> | |
namespace util { | |
template <typename T> | |
class unique_ptr { | |
public: |
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
;;; 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)))))) |
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
module Combinatorial ( | |
combine, | |
permutations, | |
matrices, | |
) where | |
-- inserts a value in a list at every possible position | |
combine :: t -> [t] -> [[t]] | |
combine x [] = [[x]] |
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
(import (scheme base)) | |
(define-syntax hornify | |
(syntax-rules () | |
((_ x an) an) | |
((_ x a pn ...) | |
(+ a (* x (hornify x pn ...)))))) | |
(define-syntax poly | |
(syntax-rules () |
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
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 |
OlderNewer