Skip to content

Instantly share code, notes, and snippets.

View wyager's full-sized avatar

Will Yager wyager

View GitHub Profile
### Keybase proof
I hereby claim:
* I am wyager on github.
* I am wyager (https://keybase.io/wyager) on keybase.
* I have a public key whose fingerprint is 4A03 3876 A021 A868 B64E 9474 3523 A9A0 071C 3DD6
To claim this, I am signing this object:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import Data.Word
import Data.Array.ST
import Data.Array.Base (unsafeRead, unsafeWrite)
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeIndex)
{-# LANGUAGE BangPatterns #-}
fib' :: Int -> Int -> Int -> Int
fib' !0 !a !_ = a
fib' !n !a !b = fib' (n-1) (b) (a+b)
fib :: Int -> Int
fib !n = fib' n 0 1
main = print $ fib (1000*1000*1000)
@wyager
wyager / gist:c1a9075ff80ee29840e1
Created December 20, 2014 21:33
Simple PID controller simulator
// PV :: process variable. Current measurement
// SP :: Setpoint. Desired measurement.
#include <math.h>
#include <stdio.h>
typedef struct {
double previous;
} derivative_context;
import Data.Map (Map, fromList, toList, unionWith, adjust, insert)
import Data.List (isPrefixOf)
data Area = Savings | Food | FoodSavings | Investments | Expenses | Play deriving (Show, Read, Ord, Eq, Enum)
data Entry = Expense Area Integer | Credit Integer | Plan [(Area, Integer)] | Comment deriving (Show, Read)
type Savings = Map Area Integer
type Plan = Map Area Integer
noSavings = fromList [(area, 0) | area <- [Savings .. Play]]
defaultPlan = fromList $ (Savings, 100) : [(area, 0) | area <- [Food .. Play]]
{-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
import Prelude hiding (writeFile)
import Control.Monad.ST (ST, runST)
import Data.Array (Array)
import Data.Array.IO (IOArray)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray, bounds, (!))
import Data.Array.ST (STArray, runSTArray, runSTUArray)
import Data.Array.MArray (MArray, newArray, getBounds, freeze, readArray, writeArray)
-- A first exploration into ST freeze/thaw fusion.
-- will.yager@gmail.com
{-# LANGUAGE RankNTypes, MultiParamTypeClasses, BangPatterns #-}
import Control.Monad.ST (ST, runST)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as M
import Control.Monad.Primitive (PrimMonad, PrimState)
@wyager
wyager / constants.hs
Created December 18, 2015 21:27
Getting lots of digits of constants
import Prelude hiding (pi)
import Data.Ratio ((%))
import Data.List(findIndex, splitAt)
-- Thanks to augustss on stackoverflow
showRational :: Int -> Rational -> String
showRational n r =
let d = round (abs r * 10^n)
s = show (d :: Integer)
-- Monoid
class Ding a where
dong : a -> a -> a
-- Verified monoid
class Ding a => VDing a where
law1 : (x,y,z : a) -> dong x (dong y z) = dong (dong x y) z
instance Ding Nat where
dong = (+)
-- Reaktor orbital challenge solution
-- Will Yager
import Data.Map (Map, keys, (!), fromList)
import Data.Set (Set, empty, notMember, insert)
import Data.List (find)
import Data.List.Split (splitOn)
data Cartesian = Cartesian Double Double Double deriving (Eq, Show)
data Polar = Polar {lat :: Double, lon :: Double, alt :: Double} deriving (Show)
data Line = Line {a :: Cartesian, b :: Cartesian}
data ID = Start | ID Int | End deriving (Eq, Ord, Show)