Skip to content

Instantly share code, notes, and snippets.

@dminuoso

dminuoso/f.hs Secret

Last active October 25, 2022 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminuoso/1e76c685d5168d8fe8881de0230a5208 to your computer and use it in GitHub Desktop.
Save dminuoso/1e76c685d5168d8fe8881de0230a5208 to your computer and use it in GitHub Desktop.
spreadBits :: Int -> Int -> [Int]
spreadBits n m = go [] 0.5 0
where
go :: [Int] -> Float -> Int -> [Int]
go result leapTotal i | i <= n
= go (whole + leap : result) leapTotal'' (i + 1)
| otherwise
= reverse result
where
leapTotal' = leapTotal + fraction
(leap :: Int, leapTotal'') = properFraction leapTotal'
zeroGroupSize :: Float
zeroGroupSize = ((/) `on` fromIntegral) (m - n) (n + 1)
(whole :: Int, fraction :: Float) = properFraction zeroGroupSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment