Last active
February 21, 2017 17:55
-
-
Save Wizek/d5807fdd3b2f67622dd9b5a0b6187b40 to your computer and use it in GitHub Desktop.
Printed datatype for Haskell: custom format deep within a data structure
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 Text.Printf | |
import ComposeLTR | |
main = do | |
print ls | |
print lsd | |
-- [26,56,114,222,398,628,857,1033,1141,1199,1229,1243] | |
-- [26,30,58,108,176,230,229,176,108,58,30,14] | |
ln | |
print $ map (Printed "%5d") ls | |
print $ map (Printed "%5d") lsd | |
-- [ 26, 56, 114, 222, 398, 628, 857, 1033, 1141, 1199, 1229, 1243] | |
-- [ 26, 30, 58, 108, 176, 230, 229, 176, 108, 58, 30, 14] | |
ln | |
print $ map (CShow $ printf "%5d") ls | |
print $ map (CShow $ printf "%5d") lsd | |
-- [ 26, 56, 114, 222, 398, 628, 857, 1033, 1141, 1199, 1229, 1243] | |
-- [ 26, 30, 58, 108, 176, 230, 229, 176, 108, 58, 30, 14] | |
data Printed a = Printed String a | |
instance PrintfArg a => Show (Printed a) where | |
show (Printed f a) = printf f a | |
data CShow a = CShow (a -> String) a | |
instance Show (CShow a) where | |
show (CShow f a) = f a | |
end = 12 | |
ls :: [Int] | |
ls = map (\x -> sg 10000 ((x / end - 0.5)) * (3200 - 1945) $> round) [1 .. end] | |
lsd = zipWith (-) ls (0 : ls) | |
sg a t = 1 / (1 + a ** (-t)) | |
ln = putStrLn "" | |
noop :: Monad m => m () | |
noop = return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment