Skip to content

Instantly share code, notes, and snippets.

View philtomson's full-sized avatar

Phil Tomson philtomson

View GitHub Profile
@philtomson
philtomson / puzzle_2.ml
Created May 2, 2012 06:06
puzzle_2.ml - operations go from left to right instead of operator precedence
(* Inspired by this puzzle from NPR:
http://www.npr.org/2012/01/29/146034893/this-puzzle-is-the-pits
(see the 'Next Weeks' challenge section there):
"Next Week's Challenge from listener Ed Pegg Jr.: Write the digits from
1 to 9 in a line. If you put times signs after the 2 and 4, a plus
sign after the 5, and a minus sign after the 7, you have
12 x 34 x 5 + 67 - 89, which equals 2018.
That's six years off from our current year 2012. This example uses
four arithmetic symbols. The object is to use just three of the
following arithmetic operations: addition, subtraction, multiplication
@philtomson
philtomson / test.ml
Created April 15, 2012 17:05
Prototyping the functors for FSM
(*ocamlc -o test test.ml*)
(* the problem we're trying to solve is to have the FSM module
* be able to deal with different expression types
*)
module type BASETYPE =
sig
type 'a var_t
val set : 'a var_t -> 'a -> unit
val get_var_name : 'a var_t -> string
val get_var_val : 'a var_t -> 'a
@philtomson
philtomson / roulette.ml
Created March 5, 2012 22:56
Random proportional selection ideas
(*
In Ruby:
Say that you have a hash 'events' whose keys are the events you are
interested in and the values are the corresponding weights:
e.g.
events = {
"event1" => 5,
@philtomson
philtomson / puzzle.ml
Created March 3, 2012 20:30
Solve a puzzle posed on NPR (see: http://www.npr.org/2012/01/29/146034893/this-puzzle-is-the-pits - "Next Week" section )
(* Inspired by this puzzle from NPR:
http://www.npr.org/2012/01/29/146034893/this-puzzle-is-the-pits
(see the 'Next Weeks' challenge section there):
"Next Week's Challenge from listener Ed Pegg Jr.: Write the digits from
1 to 9 in a line. If you put times signs after the 2 and 4, a plus
sign after the 5, and a minus sign after the 7, you have
12 x 34 x 5 + 67 - 89, which equals 2018.
That's six years off from our current year 2012. This example uses
four arithmetic symbols. The object is to use just three of the
following arithmetic operations: addition, subtraction, multiplication
@philtomson
philtomson / gist:1475348
Created December 14, 2011 05:08
functorial FSM
open Logic
type ('pred, 'ns) p_a_n = { pred: 'pred;
actions: (bexp*boolean) list;
ns: 'ns } deriving(Show);;
module type STATES =
sig
type t
@philtomson
philtomson / gist:1425225
Created December 2, 2011 22:55
Using the Functorial interface to HashTbl
(* ( 'cs, 'conditions, 'actions, 'ns) *)
(* given a current state (cs)
* and condition - get the resulting actions and next state ('ns) *)
type 'a state = State of 'a ;;
type ('cs, 'cond, 'action, 'ns) st_entry = ST_Entry of ('cs * 'cond * 'action * 'ns);;
type state_list = st_entry list ;;
type ('cs, 'cond) st_cond = State_Cond of ('cs * 'cond) ;;
type ('action, 'ns) st_action_ns = Action_NS of ('action * 'ns);;
@philtomson
philtomson / gist:1021309
Created June 12, 2011 06:39
FizzBuzz OCaml
let fizzbuzz i = match ((i mod 3), (i mod 5)) with
| (0,0) -> "fizzbuzz"
| (_,0) -> "buzz"
| (0,_) -> "fizz"
| (_,_) -> string_of_int i ;;
let do_fizzbuzz n = for i = 1 to n do
Printf.printf "%s\n" (fizzbuzz i)
done ;;
@philtomson
philtomson / gist:960272
Created May 7, 2011 06:55
reverse_words in a string (OCaml)
let reverse_words str =
let slst = Str.split (Str.regexp_string " ") str in
List.fold_left (fun accum i -> accum ^ " " ^ i) " " (List.rev slst) ;;
@philtomson
philtomson / gist:960227
Created May 7, 2011 05:34
string reverse in ocaml
let string_rev str =
let rec aux idx = match idx with
0 -> Char.escaped (str.[0])
| _ -> (Char.escaped str.[idx]) ^ (aux (idx-1)) in
aux ((String.length str)-1) ;;
set tabstop=2
set smarttab
set shiftwidth=2
set autoindent
set expandtab
set backspace=start,indent
set guifont=Inconsolata\ 14
set hlsearch
syntax on
autocmd BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") |