Instantly share code, notes, and snippets.

# olligobber olligobber

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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 {-# LANGUAGE RankNTypes #-} {-# LANGUAGE FlexibleInstances #-} module ModArith ( WithMod , IntMod , doMod ) where import Control.Applicative (liftA2)
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 {-# 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)
Last active August 4, 2021 11:58
Lists with type specified length
View FixedList.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 {-# LANGUAGE TypeOperators #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE NoStarIsType #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
Created July 6, 2021 11:12
Latex is a lazy functional language
View test.tex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 \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
Created May 4, 2021 08:51
MWE for when AsSet breaks instances
View main.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE FlexibleContexts #-} import Data.Type.Set (AsSet) class GoodType t data Type1
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 ι(ι(ι(ιι)))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ιι)(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ιι))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ιι))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ιι))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))))))))))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι))(ι(ι(ι(ιι)))(ι(ι(ιι)
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 {-# LANGUAGE TemplateHaskell #-} module Functions where import FunctionsTemplate (generateFunctions) generateFunctions
Last active February 10, 2021 09:15
View magic.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 -- 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. -}
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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