Skip to content

Instantly share code, notes, and snippets.

View lojic's full-sized avatar

Brian Adkins lojic

View GitHub Profile
@lojic
lojic / gist:5892340
Last active December 19, 2015 03:48
module MicroBlogger
def self.start
loop do
printf "enter command: "
Cmd.execute(gets.chomp)
end
end
module Cmd
def self.commands
@lojic
lojic / point_in_convex_poly.hs
Last active December 24, 2015 03:08
Point in convex polygon detection in Haskell
type Edge = (Point, Point)
type Point = (Int, Int)
type Poly = [ Point ]
type Vector = (Int, Int)
crossProduct :: Vector -> Vector -> Int
crossProduct (x1,y1) (x2,y2) = (x1*y2) - (y1*x2)
-- Transform a list of points into a list of edges
polyEdges :: Poly -> [ Edge ]
@lojic
lojic / edit_distance.hs
Last active March 27, 2019 18:16
Simple Haskell example to compute the "edit distance" between two strings. I also translated the Haskell program into Ruby as a comparison. The Ruby version is about 21x slower (when transforming "mountain lion" to "mavericks"). The algorithm is naive & inefficient, but it's the same algorithm in both languages.
-- From Simon Thompson's excellent "The Craft of Functional Programming"
--
-- Find the "edit distance" between two strings using five basic
-- editing operations:
--
-- 1) Change one character into another
-- 2) Copy a character
-- 3) Delete a character
-- 4) Insert a character
-- 5) Delete (kill) to the end of the string
@lojic
lojic / foo.rkt
Created July 22, 2014 20:53
contract example
;; Step 1
(define (valid? a)
(< a 10))
(define/contract (foo a b)
(->i ([x valid?] [y any/c]) any)
(+ a b))
;; Step 2
(define (valid? max a)
@lojic
lojic / a_peg_board.ml
Last active August 29, 2015 14:06
Obligatory Cracker Barrel peg board puzzle in OCaml
(* Solve the Cracker Barrel Peg Board Puzzle in OCaml *)
open Core.Std
open Core.Core_list
let isOccupied b p = mem b p
let isEmpty b p = not (isOccupied b p)
let isPos (r,c) = r >= 0 && r < 5 && c >= 0 && c <= r
(* Possible moves for one position *)
let positionMoves b p = let (r,c) = p in
@lojic
lojic / gilded_rose.rb
Last active August 29, 2015 14:16
Functional version of gilded rose kata. See https://github.com/jimweirich/gilded_rose_kata
module ItemAging
AGING_FUNCTIONS = {
'NORMAL ITEM' => ->(s,q) { normal(s,q) },
'Aged Brie' => ->(s,q) { brie(s,q) },
'Backstage passes to a TAFKAL80ETC concert' => ->(s,q) { backstage(s,q) },
'Conjured Mana Cake' => ->(s,q) { conjured(s,q) },
}
# A convenience function. The *only* function in the module that
# mutates non-local state.
@lojic
lojic / WestminsterShorterCatechism.txt
Created July 24, 2015 13:53
A plain text version of the Westminster Shorter Catechism with original proof texts.
The Westminster Shorter Catechism
=================================
The original text of 1647, with the Assembly's proof texts.
1. What is the chief end of man?
Man's chief end is to glorify God,(1) and to enjoy him for ever.(2)
(1) I Cor. 10:31; Rom. 11:36. [ Ps 86; Is 60:21; Rev 4:11 ]
@lojic
lojic / spelling-corrector.rkt
Last active October 12, 2015 21:08
First draft of a port of Peter Norvig's spelling corrector to Racket
#lang racket
; Thanks to Vincent St-Amour and Sam Tobin-Hochstadt for their tips on #racket
(define (words text)
(regexp-match* #rx"[a-z]+" (string-downcase text)))
(define (train features)
(define model (make-hash))
(for ([f features])
@lojic
lojic / haskell.hs
Created October 15, 2015 14:07
Deletion: The curse of the red-black tree. Kimball Germane and Matthew Might. Comparison of Racket and Haskell implementations.
data Color = R | B | BB deriving (Show)
data Tree elt = E | EE | T Color (Tree elt) elt (Tree elt) deriving (Show)
type Set a = Tree a
empty = E
insert x s = blacken (ins s)
where insE=TRExE
ins (T color a y b) | x < y = balance color (ins a) y b
#lang racket
(define (memoize proc)
(define memo '())
(λ (x)
(cond [(assq x memo) => cdr]
[else (let ([result (proc x)])
(set! memo (cons (cons x result) memo))
result)])))