Skip to content

Instantly share code, notes, and snippets.

View Sintrastes's full-sized avatar

Nathan BeDell Sintrastes

View GitHub Profile
-- Some code for parsing natural language, transforming grammar trees into statements in
-- propositional logic, and infering things from those propositions.
--
-- This code is pretty unstructured, but I might salvage some of some day.
-- I'm leaving this here on the off chance that someone stumbles upon this and finds it
-- useful, enjoy!
module Main where
import Data.List.Split
first (x,_,_) = x
second (_,y,_) = y
third (_,_,z) = z
filterFirst p x = p (first x)
filterSecond p x = p (second x)
filterThird p x = p (third x)
exists :: Int -> (a -> Bool) -> [(a,a,a)] -> [(a,a,a)]
exists 0 p xs = filter (filterFirst p) xs
@Sintrastes
Sintrastes / golf.hs
Last active February 13, 2020 14:37
golf.hs
-- Outputs the number of strings of length six with the digits 0-9 with only two numbers used
-- (e.a. "100110", "611661", "899889") in 119 characters of Haskell. Enjoy!
import Data.List
z x|length x<6=z("0"++x)|length x==6=x
main=print$length$filter((==2).length)$map(nub.z.show)[0..999999]
{-# LANGUAGE ViewPatterns #-}
module Main where
import Data.Ratio
-- Runs the euclidian algorithm, returning the GCD along with a list of quotients,
-- or equivantley, a continued fraction representation of a/b in list form
euclidGCD :: (Integral a) => a -> a -> (a,[a])
euclidGCD a b = go a b []
-- Saw a (joke?) stack exchange post about an extensible fizzbuzz implementation.
-- I was bored, so I thought I'd give my take on something similar in Haskell:
import qualified Data.Map.Strict as M
import Control.Monad (join)
fizzBuzz :: M.Map Int String -> Int -> String -> [String]
fizzBuzz dict n default_value = map (toWords . divisors) [1..n]
where keys = M.keys dict
divisors n = filter (\x -> (n `mod` x) == 0) keys
@Sintrastes
Sintrastes / normtest.r
Last active August 29, 2015 14:14
normtest.r
# Visual single variable normality test plot
# Useful for testing normality assumptions in DOX.
normTest <- function(data){
n <- length(data)
expected <- pnorm((1:n)/(n+1))
plot(x=expected,y=sort(data),xlab="Expected Norm. Dist.",ylab="Data")
}
-- Long binary division algorithm, used for the CSCI 342 final project (floating point emulation).
-- Pro tip: Apparently integer division works just as well as this if you do some bit shifting.
longBinaryDivision :: Integer -> Integer -> [Bool]
longBinaryDivision num denom = go (createDivadend num) denom num 0
where go :: [Bool] -> Word32 -> Word32 -> Int -> [Bool]
go divadend divisor remainder count
| divisor == remainder = [True]
| divisor < remainder = [True] ++ go (tail divadend) divisor (carryDown (remainder - divisor)) (count +1)
| divisor > remainder = [False] ++ go (tail divadend) divisor (carryDown remainder) (count +1)
-- Linear correlation stuff for MATH 211, yay?
-- An error, but a bit more condescending than usual.
(ಠ_ಠ) x = error ("ಠ_ಠ "++x++" ಠ_ಠ")
ccoeff :: [Double] -> [Double] -> Double
ccoeff xs ys = (n*xyΣ - xΣ*yΣ)/((n*x²Σ - xΣ**2.0)*(n*y²Σ-yΣ**2.0))**0.5
where xΣ = sum xs
yΣ = sum ys
xyΣ = sum $ zipWith (*) xs ys
-- Simple time module, no fuss over real world complications, just a simple way
-- to calculate sums and differenes of hours and minutes on a 12 hour
-- clock using divs and mods.
module SimpleTime where
-- Time in a simple hour:minute format
data Time = Time Int Int
-- Helper function to display times correctly
@Sintrastes
Sintrastes / CalkinWilf.hs
Created September 16, 2019 20:01
An implementation of the Calkin-Wilf sequence in Haskell
--
-- An implementation of the Calkin-Wilf sequence in Haskell -- gives a bijection between the natural numbers
-- and the (positive) rationals.
--
-- See https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree for more info
--
import Data.Ratio
calkinWilfNext q = 1 / (2 * ( (floor q) % 1 ) - q + 1)