Skip to content

Instantly share code, notes, and snippets.

quicksort :: Ord a => [a] -> [a]
quicksort = quicksortBy compare
quicksortBy :: (a -> a -> Ordering) -> [a] -> [a]
quicksortBy _ [] = []
quicksortBy _ [x] = [x]
quicksortBy cmp xs = (quicksortBy cmp lo) ++ [pivot] ++ (quicksortBy cmp hi) where
(pivot, (lo, hi)) = partitionBy cmp xs
partitionBy :: (a -> a -> Ordering) -> [a] -> (a, ([a], [a]))
@LivewareIssue
LivewareIssue / TreeTraversal.hs
Created May 21, 2020 21:29
Depth-first and breadth-first traversals of rose-trees
data Tree a = Empty | Node a [Tree a]
deriving Show
dft :: Tree a -> [a]
dft Empty = []
dft (Node x xs) = x:(concatMap dft xs)
bft :: Tree a -> [a]
bft Empty = []
bft (Node x xs) = x:(catMaybes $ map value xs) ++ (concatMap bft (concatMap children xs))
@LivewareIssue
LivewareIssue / Mergesort.hs
Created May 21, 2020 21:27
Mergesort via hylomorphism
mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy _ [] [] = []
mergeBy _ xs [] = xs
mergeBy _ [] ys = ys
mergeBy cmp xs@(x:xs') ys@(y:ys')
| cmp x y <= EQ = x:(mergeBy cmp xs' ys)
| otherwise = y:(mergeBy cmp xs ys')
merge :: Ord a => [a] -> [a] -> [a]
merge = mergeBy compare
@LivewareIssue
LivewareIssue / Mergesort.hs
Created May 21, 2020 21:24
Bottom-up implementation of mergesort
mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy _ [] [] = []
mergeBy _ xs [] = xs
mergeBy _ [] ys = ys
mergeBy cmp xs@(x:xs') ys@(y:ys')
| cmp x y <= EQ = x:(mergeBy cmp xs' ys)
| otherwise = y:(mergeBy cmp xs ys')
merge :: Ord a => [a] -> [a] -> [a]
merge = mergeBy compare
@LivewareIssue
LivewareIssue / Mergesort.hs
Created May 21, 2020 21:23
Top-down implementation of mergesort
mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy _ [] [] = []
mergeBy _ xs [] = xs
mergeBy _ [] ys = ys
mergeBy cmp xs@(x:xs') ys@(y:ys')
| cmp x y <= EQ = x:(mergeBy cmp xs' ys)
| otherwise = y:(mergeBy cmp xs ys')
merge :: Ord a => [a] -> [a] -> [a]
merge = mergeBy compare
@LivewareIssue
LivewareIssue / MaximumPopulationYear.hs
Created May 21, 2020 21:09
Given a collection of lifespans, represented as a list of (year-of-birth, year-of-death) pairs, determine in which year the population was greatest
import Test.QuickCheck
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Ord (comparing)
import Data.List (sortBy, maximumBy)
main :: Int -> IO ()
main n = do
lifespans <- generate $ arbitrary_lifespans n
print $ maximumPopulationYear lifespans
@LivewareIssue
LivewareIssue / RealEstateBroker.hs
Last active May 21, 2020 21:40
Maximal bipartite matching via Ford-Fulkerson
data Edge a b = Edge { source ∷ a, sink ∷ a, label ∷ b }
deriving (Eq, Show)
-- Directed, edge-labelled graph represented as an adjacency list
type Graph a b = [(a, [Edge a b])]
-- A path is a contigious sequence of edges
type Path a b = [Edge a b]
-- Get all the edges of a graph
@LivewareIssue
LivewareIssue / CaseConversion.vb
Created March 12, 2020 16:49
Case Conversion
Imports System.Data.Entity.Design.PluralizationServices
Imports System.Globalization
Imports System.Runtime.CompilerServices
Imports Sprache
Public Module Program
Public Sub Main(args As String())
Dim identifier = "Sage200HTMLPerson"
PascalCase.ToKebabCase("FooBaa")
@LivewareIssue
LivewareIssue / GetSchema.sql
Created March 3, 2020 19:04
Schema Retrieval SQL Command
SELECT
ColumName = col.name,
DataType = typ.name,
[Precision] = col.precision,
[Scale] = col.scale,
[MaxLength] = col.max_length,
IsPrimaryKey = ISNULL(ix.is_primary_key, 0),
ReferencedTableName = tbl.name,
ReferencedColumnName = refcol.name
FROM
@LivewareIssue
LivewareIssue / GameOfLife.hs
Created February 27, 2020 16:17
Game of Life
{-# LANGUAGE PolyKinds, PartialTypeSignatures #-}
module GameOfLife where
import Control.Comonad (extract, extend)
import Control.Comonad.Representable.Store (Store, peek, store, experiment)
import Data.Functor.Rep (Rep, index)
import Data.Finite (Finite, strengthen, shift, unshift, weaken)
import Data.Functor.Compose (Compose)
import GHC.TypeLits (Nat, KnownNat, type (<=))