Skip to content

Instantly share code, notes, and snippets.

@bmjames
bmjames / skicalc.prolog
Created January 3, 2011 18:36
Prolog version of the SKI calculus evaluator
%% -*-mode: prolog-*-
%% Example of the reversal operator:
%% ?- R = [[s, [k, [s, i]]], k],
%% eval([[R, a], b], Result).
%%
%% R = [[s,[k,[s,i]]],k]
%% Result = [b,a] ?
%% eval delegates to eval1 to perform the transformations of the calculus,
@bmjames
bmjames / skicalc.erl
Created December 17, 2010 17:11
Evaluator for SKI combinator calculus trees
-module(skicalc).
-author("Ben James <[email protected]>").
-export([eval/1, conv/1]).
% Valid terms to be evaluated here are binary trees represented as
% two-element tuples. Leaves can be anything, and the atoms s, k and i
% represent the symbols S, K and I in the calculus' alphabet.
% For example, an operator to reverse two elements can be defined as:
def fib_gen(first=1, second=2):
"Infinite Fibonacci generator"
while True:
next, first, second = first, second, first+second
yield next
import scala.io.Source
val words = for {
line <- Source.fromFile("/usr/share/dict/words").getLines
word = line.trim.toLowerCase
if word.length <= 6
} yield word
val sixLetterWords = words filter { _.length == 6 }
val candidateParts = words filter { _.length < 6 }
import scala.collection.mutable.{Map => MutableMap}
import scala.io.Source
class AnagramMap {
protected val anagrams = MutableMap.empty[String, Set[String]]
def add(word: String) {
val normalised = normalise(word.toLowerCase)
val equivalents = anagrams.getOrElseUpdate(normalised, Set.empty[String])
import scala.collection.mutable.{Set => MutableSet}
import scala.io.Source
import java.security.MessageDigest
class BloomSet(numHashes: Int) {
protected val hashes = MutableSet.empty[String]
def add(word: String) {
hashes ++= digest(word)