Skip to content

Instantly share code, notes, and snippets.

@Porges
Last active July 4, 2025 01:52
Show Gist options
  • Save Porges/3cb6a3d28371c7ccc578354c8284015b to your computer and use it in GitHub Desktop.
Save Porges/3cb6a3d28371c7ccc578354c8284015b to your computer and use it in GitHub Desktop.
extensible_list.hs
{-# LANGUAGE OverloadedStrings, FlexibleInstances #-}
import Data.String (IsString(..))
import Data.Char (ord, chr)
import Math.OEIS (extendSequence)
main :: IO ()
main = do
print ("ASDF" + 2)
print (1 + "KLMN")
data ExtensibleList a = List [a] | Amount Integer
default (ExtensibleList Char)
instance Functor ExtensibleList where
fmap f (List xs) = List (fmap f xs)
fmap f (Amount n) = Amount n
instance Num (ExtensibleList Char) where
fromInteger = Amount
x + y = toChars (toInts x + toInts y)
where
toInts = fmap (toInteger . ord)
toChars = fmap (chr . fromInteger)
instance Num (ExtensibleList Integer) where
fromInteger = Amount
(List x) + (List y) = List (x ++ y)
(List x) + (Amount n) = List (take (length x + fromInteger n) (extendSequence x))
(Amount n) + (List x) = List (reverse (take (length x + fromInteger n) (extendSequence (reverse x))))
(Amount n) + (Amount n') = Amount (n + n')
-- implement the rest yourself
instance IsString (ExtensibleList Char) where
fromString = List
instance Show (ExtensibleList Char) where
show (Amount n) = show n
show (List x) = x
instance Show (ExtensibleList Integer) where
show (Amount n) = show n
show (List x) = show x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment