Skip to content

Instantly share code, notes, and snippets.

@FMNSSun
Created Nov 5, 2015
Embed
What would you like to do?
Program to generate Deadfish constants
import Data.List
import System.Environment
sqrti :: Int -> Int
sqrti = floor . sqrt . fromIntegral
squ :: Int -> Int
squ x = x * x
squares :: [Int]
squares = map squ [0..15]
nearestSquare :: Int -> Int
nearestSquare x =
let diffs = map (abs . (x-)) squares
in squares !! (minIndex (minimum diffs) diffs)
minIndex :: Int -> [Int] -> Int
minIndex x xs =
case findIndex (==x) xs of
Just index -> index
_ -> error $ "Can't find minIndex for " ++ (show x) ++ " in " ++ (show xs)
genDf :: Int -> String
genDf 0 = ""
genDf 1 = "i"
genDf 2 = "ii"
genDf 3 = "iii"
genDf x =
let ns = nearestSquare x
diff = ns - x
dst = abs $ diff
ins = if diff == 0 then ""
else if diff > 0 then "d" else "i"
nx = sqrti ns
pins = genDf nx
in pins ++
"s" ++
(concat (replicate dst ins))
main :: IO ()
main = do
args <- getArgs
case args of
[dfn] -> putStrLn . genDf . read $ dfn
_ -> do
pn <- getProgName
putStrLn $ "Usage: " ++ pn ++ " <number>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment