Skip to content

Instantly share code, notes, and snippets.

View twfarland's full-sized avatar

Tim Farland twfarland

  • Farland Digital
  • Auckland, NZ
View GitHub Profile
@twfarland
twfarland / HTDP-exercise16.3-challenge.rkt
Created February 3, 2011 09:27
My solution for exercise 16.3 extended challenge in How to Design Programs
#lang racket
;a file is a structure (make-file n s x) where n is a symbol, s is a number, and x is some scheme value
(define-struct file (name size content))
;a dir is a structure (make-dir n ds fs) where n is a symbol and ds is a list of directories and fs is a list of files
(define-struct dir (name dirs files))
; a list of files is either empty or (cons f lof) where f is a file and lof is a list of files
@twfarland
twfarland / HTDP12.4.TF_solution.rkt
Created February 7, 2011 08:57
My solution for exercise 12.4 in HTDP
;; 12.4 Rearranging words
;; a 'word' is either
;; 1 - empty, or
;; 2 - (cons a w) where a is a symbol
;; ('a, 'b,..., 'z) and w is a word
;; examples:
(define noword empty)
(define red (cons 'r (cons 'e (cons 'd empty))))
(defun blogTemplate ()
[["!doctype html"]
['html
['head
['meta {charset 'utf-8}]
['title this.title]]
['body
['section
['h1 this.title]
['div {class 'articles} (Don.mapRender articleTemplate this.articles)]]]]])
@twfarland
twfarland / htdp_28_1_haskell.hs
Created August 27, 2011 12:43
HTDP 28.1 translated into Haskell
-- translating http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-35.html#node_chap_28 to Haskell,
-- making use of Maybe, pattern matching, and type aliases
-- 28.1 Finding routes in a cycle-free directed graph
type Node = Char
type Graph = [(Node,[Node])]
type Route = [Node]
@twfarland
twfarland / queens.hs
Created August 31, 2011 21:47
Queen placement problem from HTDP exercise 28.2
-- 28.2 - Queen problem from HTDP
-- I took a few liberties here, thought constraint propagation might be faster
-- 'placement (buildBoard x) y' where x is the width/height of the chessboard in cells
-- and y is the number of queens to be placed, returns the first solution if that many
-- queens can be placed without cross-threatening, or Nothing if there is no solution
-- a Board consists of a list of positions of free tiles, and a list of positions of placed queens
type Posn = (Int, Int)
type Queens = [Posn]
type FreeTiles = [Posn]
@twfarland
twfarland / difflists.rkt
Created January 24, 2012 03:22
Difference lists with monoid functions in Racket
#lang racket
;; ## DIFFERENCE LISTS
;; # Constructors
;; Diff a is [a] -> [a]
;; implemented as - diff :: a... -> ([a] -> [a])
(define diff
(λ ls (curry append ls)))
@twfarland
twfarland / diffsort.hs
Created January 25, 2012 05:47
diff-tree sort
{-
DIFF-TREE SORT
I did this as a learning exercise.
I assume it has been discovered before.
It is an attempted optimisation of tree sort.
It uses a naive binary tree,
but each node keeps a list of same-values,
and difference lists
@twfarland
twfarland / currycompose.coffee
Created February 10, 2012 11:05
Currying / Function composition in coffeescript
arr = Array::
arrSlice = arr.slice
curry = ->
args = arrSlice.call arguments
->
args2 = arrSlice.call arguments
args[0].apply @, args.slice(1).concat(args2)
sum = ->
@twfarland
twfarland / reddit-ranking.rkt
Created March 27, 2012 11:07
Reddit ranking algorithms in Racket
#lang racket
(require "connive.rkt")
; https://github.com/twfarland/connive
; Reddit ranking algorithms
; Story ranking (hot ranking)
; http://amix.dk/blog/post/19588
; Submission time greatly affects score
(:= epoch (current-seconds))
@twfarland
twfarland / pals.py
Created May 3, 2012 19:25
Finding the longest subpalindrome in a piece of text
# Finding the start and finish indexes of the
# longest subpalindrome of a given string.
# My solution - does war & peace in approx 3.2 sec on my 2.4GHz macbook
def longest_subpalindrome_slice(text):
text = text.lower()
textlen = len(text)