Skip to content

Instantly share code, notes, and snippets.

@csoroz
csoroz / Test.hs
Created July 10, 2019 15:55
Non-integer test
import System.Environment (getArgs)
import System.IO (IOMode(..),withFile,hGetContents)
import qualified Data.Fixed as FP
import NonIntegral
data E34
instance FP.HasResolution E34 where
resolution _ = 10^(34::Int)
@csoroz
csoroz / UnitInterval.hs
Created June 21, 2019 11:25
Unit Interval QuickCheck [0,1]
import qualified Data.Fixed as FP
import Test.QuickCheck
data E34
instance FP.HasResolution E34 where
resolution _ = 10^34
type FixedPoint = FP.Fixed E34
@csoroz
csoroz / Sheldon.hs
Created June 5, 2019 17:08
Sheldon's Theorem
-- [Sheldon's (Conjecture) Theorem](https://www.math.dartmouth.edu/~carlp/sheldonresub011119.pdf)
import Test.QuickCheck
import Data.Tuple
import Data.List
unDigits base = foldl (\a b -> a * base + b) 0
digitsRev base = unfoldr step
where
step 0 = Nothing
@csoroz
csoroz / Dijkstra.hs
Created May 17, 2019 14:52
Dijkstra's shortest path algorithm for monoids types with infinity
import Data.List
import qualified Data.List.Key as K
import Data.Map ((!), fromList, fromListWith, adjust, keys, Map)
import Data.Monoid.Inf (Inf(..),Pos,posInfty) -- cabal install monoid-extras
type T = Inf Pos Integer
type G a = Map a [(a,T)]
buildGraph :: Ord a => [(a,a,T)] -> G a
buildGraph g = fromListWith (++) (g >>= f) where
@csoroz
csoroz / Kanjis.hs
Created May 10, 2019 00:04
Enumerating Regular Expressions : Kanji numerals
import RegExp
import Data.List
-- Kanji numerals
oneOf = foldr (:|) Phi . map Single
kanji = o (u :. Single 'T')
:. o (a :. Single 'M')
:. o (a :. Single 'C')
@csoroz
csoroz / Hofstadters.hs
Created May 10, 2017 15:55
Hofstadter Figure-Figure sequences
import Data.List
-- https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences
-- Hofstadter Figure-Figure sequences [R](https://oeis.org/A005228) and [S](https://oeis.org/A030124)
-- R(0) = 1; S(0) = 2; {S(n)} = increasing series of positive integers not present in {R(n)}
-- R(n+1) = R(n) + S(n)
r = 1:zipWith (+) r s
s = 2:4:drop 2 (concat $ zipWith ss r (tail r)) where ss x y = [x+1..y-1]
main = print (take 22 r) -- [1,3,7,12,18,26,35,45,56,69,83,98,114,131,150,170,191,213,236,260,285,312]