Skip to content

Instantly share code, notes, and snippets.

type nat = Zero | Succ of nat
let rec int_of_nat x =
match x with
| Zero -> 0
| Succ x' -> 1 + int_of_nat x'
let rec nat_of_int x =
match x with
| 0 -> Zero
type t = node list
and node = Node of (char * node list) | Eow
let empty = []
let mem word nodes =
let len = String.length word in
(* Return the children nodes of the node containing the character c,
module CMap = Map.Make(struct
type t = char
let compare = Char.compare
end)
type t = Trie of (t CMap.t * bool)
let empty = Trie (CMap.empty, false)
let add word trie =
module CMap = Map.Make(struct
type t = char
let compare = Char.compare
end)
type t = Trie of (t CMap.t * bool)
let empty = Trie (CMap.empty, false)
let add word trie =
def g(x):
return x*x
def f(x):
global f
if x == 5:
f = g
return x
print [f(x) for x in xrange(10)]
(* Lexer *)
{
open Parser
}
let alpha = ['a'-'z' 'A'-'Z' '_']
let digit = ['0'-'9']
let alnum = (alpha | digit)
let intlit = digit+
let ident = alpha alnum*
import java.util.Set;
import mclint.util.Parsing;
import ast.*;
import natlab.toolkits.analysis.core.ReachingDefs;
import natlab.toolkits.analysis.core.UseDefDefUseChain;
import natlab.toolkits.filehandling.GenericFile;
import natlab.toolkits.path.FileEnvironment;
import natlab.tame.BasicTamerTool;
import natlab.tame.tir.*;
fn insertion_sort<T: PartialOrd>(xs: &mut Vec<T>) {
for i in 1..(xs.len() - 1) {
let mut j = i;
while j > 0 && xs[j-1] > xs[j] {
xs.swap(j, j-1);
j -= 1;
}
}
}
@gnuvince
gnuvince / binsearch.org
Last active April 13, 2024 17:45
Literate programming in Org-mode + Rust: binary search

Introduction

In the fourth chapter of “Programming Pearls”, Jon Bentley discusses program correctness and tells us that as part of some of his programming classes, he asks the attendees to implement the binary search algorithm. Although simple in appearance (“look at the middle element, if it’s the target terminate, if it’s smaller look in the upper half, otherwise look in the lower half”), it can be a surprisingly tricky algorithm to implement. He cites TAoCP Vol. 3 wherein Don Knuth mentions that although the first paper on binary

use std::io;
use std::collections::HashMap;
use std::collections::VecDeque;
#[derive(Debug, PartialEq, Copy, Clone)]
enum Command {
Assign { bot: usize, value: usize },
Give {
src: usize, // from which bot?
dst1: usize, // to which bot/bin?