From ab981deb57eedc35ec7bbb4f20e9deecd307123c Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Tue, 29 Dec 2020 09:41:47 -0500 Subject: better --- main.hs | 2 +- shelf.hs | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/main.hs b/main.hs index beeb0f3..a3b1dcb 100755 --- a/main.hs +++ b/main.hs @@ -1,5 +1,5 @@ #!/usr/bin/env stack -{- stack script --resolver lts-16.14 --install-ghc +{- stack script --resolver lts-16.27 --install-ghc --ghc-options -Wall --ghc-options -Wno-unused-imports -} {-# language NoImplicitPrelude #-} {-# language DuplicateRecordFields #-} diff --git a/shelf.hs b/shelf.hs index 2c27b52..61710e8 100755 --- a/shelf.hs +++ b/shelf.hs @@ -1,11 +1,32 @@ #!/usr/bin/env stack -{- stack script --resolver lts-16.14 --install-ghc +{- stack script --resolver lts-16.27 --install-ghc --ghc-options -Wall --ghc-options -Wno-unused-imports -} {-# language NoImplicitPrelude #-} {-# language DuplicateRecordFields #-} import Rebase.Prelude import Control.Lens +import Data.Ratio +import Text.Printf + +toCarpenter :: RealFrac a => a -> (Int, Int, Ratio Int) +toCarpenter l = (feet, div r 32, mod r 32 % 32) where + (feet, r) = divMod (round $ l * 32) (32 * 12) + +feetAndInches :: RealFrac a => a -> String +feetAndInches l = case toCarpenter l of + (0,0,0) -> "0 feet 0 inches" + (f,i,t) -> showUnit "foot" "feet" (f % 1) ++ + (if f > 0 && (i%1 + t) > 0 then " " else "") ++ + showUnit "inch" "inches" (i % 1 + t) + where + showUnit _ _ 0 = "" + showUnit s m n = printf "%s %s" (showVal n) $ if n <= 1 then s else m + showVal v | d == 1 = show n + | v < 1 = printf "%d/%d" n d + | otherwise = printf "%d and %d/%d" (div n d) (mod n d) d + where (n,d) = (numerator v, denominator v) + type Inches = Rational boardLength, boardThickness, kickerHeight, numShelves, numHorizontals, availableSpace :: Inches @@ -18,11 +39,20 @@ numHorizontals = numShelves + 1 availableSpace = boardLength - kickerHeight - numHorizontals * boardThickness shelves :: [Rational] -shelves = [2%9, 2%9, 2%9, 1%6, 1%6] +shelves = kickerHeight : map (* availableSpace) [2%9, 2%9, 2%9, 1%6, 1%6] + +positions :: [Inches] +positions = map (+ boardThickness) $ map sum $ tails shelves +positions' = map (boardLength -) positions main :: IO () main = do _ <- return $ assert $ sum shelves == 1 print shelves + print positions + print $ map (fromRational :: Rational -> Float) positions + print positions' + print $ map (fromRational :: Rational -> Float) positions' + mapM_ putStrLn $ feetAndInches <$> positions' return () -- cgit v1.2.3