Skip to content

Instantly share code, notes, and snippets.

@olligobber
olligobber / Bonding.hs
Created September 15, 2023 23:10
Bonding (xkcd.com/1188) for haskell
{-# LANGUAGE RecursiveDo #-}
import Control.Concurrent (ThreadId, throwTo, yield, forkIO)
import Control.Exception (Exception, catch)
import Control.Monad (forever)
wait :: IO ()
wait = forever yield
data Ball = Ball deriving Show
@olligobber
olligobber / _rot_out
Created July 13, 2023 07:26
Find all sets of words that are equivalent under Caesar cipher
Loaded dictionary with 82222 words
Looping over 484 sets of matches
Set number 1
a
b
c
d
e
f
g
@olligobber
olligobber / output
Last active July 12, 2023 13:06
Find the longest set of words that are rotation ciphered versions of each other
Loaded dictionary with 82222 words
Longest match has length: 7
Looping over 1 sets of matches
Set number 1
abjurer
nowhere
@olligobber
olligobber / 1Main.hs
Created May 11, 2023 13:21
Explains the process of polynomial long division
import Poly (Field, inverse, Poly, x, polyDivModW)
import Control.Monad.Trans.Writer (execWriter)
newtype F2 = F2 Int
deriving (Eq)
instance Show F2 where
show (F2 x) = show x
instance Num F2 where
@olligobber
olligobber / ModArith.hs
Created November 29, 2021 04:55
A type for doing modulo arithmetic that forces all calculations to use the same mod at a type level, but allows the mod to be decided by a value
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
module ModArith
( WithMod
, IntMod
, doMod
) where
import Control.Applicative (liftA2)
@olligobber
olligobber / aio2021q5.hs
Last active August 29, 2021 10:55
Finds the largest distance between two array elements that add to at most a certain total
import Control.Monad.Tardis
import Data.Function (on)
import Control.Monad (replicateM, zipWithM, liftM, ap)
import Data.Foldable (toList)
import System.IO (openFile, IOMode(ReadMode), hGetLine)
import Control.Monad.Fix (MonadFix(..))
import Control.Monad.Trans (MonadTrans(..))
import Data.Functor.Identity (Identity(runIdentity))
import Data.List (uncons)
@olligobber
olligobber / primechess.hs
Last active August 4, 2021 12:01
Solve a simple problem (how many ways can you place 4 things in a 3x3 grid so none are adjacent) using a non-deterministic stateful monad and fixed length lists
{-# LANGUAGE DataKinds #-}
import Prelude hiding (iterate)
import Control.Monad.State (StateT, execStateT, get, gets, modify)
import Control.Arrow ((>>>))
import Data.Functor.Compose (Compose(Compose))
import Data.List (sort, nub)
import Control.Monad (when, guard)
import Control.Monad.Trans (lift)
@olligobber
olligobber / FixedList.hs
Last active August 4, 2021 11:58
Lists with type specified length
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoStarIsType #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
@olligobber
olligobber / test.tex
Created July 6, 2021 11:12
Latex is a lazy functional language
\documentclass[a4paper]{article}
\def \error{\def\error1{}\error2}
\def \S#1#2#3{#1#3{#2#3}}
\def \K#1#2{#1}
\def \i#1{#1\S\K}
\begin{document}
\i{\i{\i\i}}1\error % Does not error
@olligobber
olligobber / main.hs
Created May 4, 2021 08:51
MWE for when AsSet breaks instances
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
import Data.Type.Set (AsSet)
class GoodType t
data Type1