Skip to content

Instantly share code, notes, and snippets.

@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
View ModArith.hs
{-# 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
View aio2021q5.hs
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
View primechess.hs
{-# 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
View FixedList.hs
{-# 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
View test.tex
\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
View main.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
import Data.Type.Set (AsSet)
class GoodType t
data Type1
@olligobber
olligobber / printByte.iota
Last active March 16, 2021 11:03
Prints a byte, encoded as an 8-tuple of booleans, using 2 variables to represent it in hexadecimal
View printByte.iota
ι(ι(ι(ιι)))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ιι)(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ιι))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ιι))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ιι))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι)
@olligobber
olligobber / Functions.hs
Created February 10, 2021 12:41
Generates and exports infinitely many functions (uses all your ram at compile time) with template haskell
View Functions.hs
{-# LANGUAGE TemplateHaskell #-}
module Functions where
import FunctionsTemplate (generateFunctions)
generateFunctions
View magic.hs
-- Normal if statement, requires brackets or associative operators for nesting.
if' :: Bool -> a -> a -> a
if' b x y = if b then x else y
{-
Magic if statement, no brackets needed.
`magicIf` and `magicElseIf` pass on a function, that is either `id` or
`const x` saying what to do when a True or Else case is reached, to the next
`magicElseIf` or `magicElse`.
-}
@olligobber
olligobber / 2Stamp.hs
Last active March 30, 2020 07:11
How many 1c, 7c, or 10c stamps do you need to total a given value
View 2Stamp.hs
import qualified DP
import Data.Map ((!))
nextSubproblem :: DP.DPCalc Int Int () Int
nextSubproblem = do
this <- DP.thisSubproblem
return (this + 1)
subproblemSolver :: DP.DPCalc Int Int () Int
subproblemSolver = do