Skip to content

Instantly share code, notes, and snippets.

Avatar

Daniel Buckmaster crabmusket

View GitHub Profile
@crabmusket
crabmusket / Cheryl.hs
Last active Aug 29, 2015
A translation of Norvig's solution to the Cheryl's Birthday problem from Python to Haskell. Original: http://nbviewer.ipython.org/url/norvig.com/ipython/Cheryl.ipynb
View Cheryl.hs
-- A list of possible dates Cheryl's birthday might be on.
possibilities = [(May, 15), (May, 16), (May, 19),
(June, 17), (June, 18),
(July, 14), (July, 16),
(August, 14), (August, 15), (August, 17)]
-- We say we know the actual date when the list of possibilities is singular.
know ps = length ps == 1
-- Telling someone the month or day will reduce the possibilities.
@crabmusket
crabmusket / mercuryTree.js
Last active Aug 29, 2015
Rendering a recursive tree of items with mercury
View mercuryTree.js
'use strict';
// Mercury is a 'truly modular frontend framework'. This helpful import gets
// us a lot of top-level symbols from submodules it re-exports.
var hg = require('mercury');
// Like this one - h is a short constructor for HTML elements.
var h = require('mercury').h;
// And since we'll be running this in a browser using beefy, we need a reference
// to the document.
var document = require('global/document');
@crabmusket
crabmusket / StressAnalysis.hs
Last active Oct 12, 2015
Analysing mechanical stresses in Haskell.
View StressAnalysis.hs
-- Define a module with the same name as the file.
module StressAnalysis where
-- A Beam is defined by its geometry. Think of this as a class declaration.
data Beam = Circle {
diameter :: Float -- Member named 'diameter' of type 'Float'
} deriving (Show)
-- Define functions for common properties of beams. The functions are
-- impleented down the bottom of the file. These functions take a Beam and
@crabmusket
crabmusket / AssignmentE.hs
Created Nov 1, 2012
MECH2400 assignment code.
View AssignmentE.hs
-- Main module so that this file can be compiled.
module Main where
-- Import module defined in StressAnalysis.hs (https://gist.github.com/3979818)
import StressAnalysis
-- Main function detemermines what the compiled executable does.
-- At the moment, I want to graph the stress at each element of a section.
main = graphElements
@crabmusket
crabmusket / stepResponse.py
Last active Dec 15, 2015
Step responses of LTI systems in Python for AMME3500.
View stepResponse.py
# Step response graphs
# Python 2.7 with numpy, scipy, and matplotlib
# Based on https://gist.github.com/ofan666/1882562
from numpy import min as nmin
from scipy import linspace
from scipy.signal import lti, step
from matplotlib import pyplot as p
def plotLTI(num, den, n = 200):
@crabmusket
crabmusket / moreStepResponse.py
Created Apr 8, 2013
Step response of a complex LTI system plotted with several parameter variations in Python.
View moreStepResponse.py
# Step response graphs
# Python 2.7 with numpy, scipy, and matplotlib
# Based on https://gist.github.com/ofan666/1882562
from numpy import min as nmin
from scipy import linspace
from scipy.signal import lti, step
from matplotlib import pyplot as p
def create_tf(i):
@crabmusket
crabmusket / ReadNCFile.hs
Last active Dec 16, 2015
Reads the G commands from a CNC file and outputs the physical state of the machine after each command.
View ReadNCFile.hs
module Main where
import Data.Maybe
import Text.Printf
import Text.Regex.Posix
-- The main function defines what the program does when we compile it.
main = do
-- Read all data from standard input (until the end of the file).
file <- getContents
@crabmusket
crabmusket / DistanceScaling.hs
Last active Dec 16, 2015
Exponentially scaling distances for rendering objects that are far away using an imprecise floating Z buffer.
View DistanceScaling.hs
module Main where
import Graphics.Gnuplot.Simple
-- Calculates the scaling factor for an object at distance 'dist'. The object
-- will be at scale 1 until 'max'/2, and then will be exponentially scaled down
-- as it increases. As 'dist' approaches infinity, 'dist * factor max dist'
-- approaches 'max'. See http://goo.gl/xLzrv
factor max dist = if dist < max / 2
then 1
else (1 - 1 / 2**(f+1)) / f
@crabmusket
crabmusket / monitor.hs
Created May 16, 2013
Monitor a directory to rebuild pandoc files automatically.
View monitor.hs
module Main where
import Filesystem (getWorkingDirectory)
import Filesystem.Path (extensions)
import System.FSNotify
import System.Exit
import System.Environment
import System.Cmd
import Control.Concurrent
import Control.Monad
@crabmusket
crabmusket / controllability.hs
Last active Dec 17, 2015
Controllability of a system described with a state equation.
View controllability.hs
module Main where
import Control.Monad (liftM)
import Numeric.LinearAlgebra (readMatrix, multiply, rank, cols, rows, fromBlocks)
main = do
putStrLn "Enter matrix A (in format \"1 2 3; 4 5 6; 7 8 9\")"
a <- getMatrix
putStrLn "Enter matrix B (in format \"1; 2; 3\")"