From 8f811cd88ccafd6d7a05e8c5aaaaa78d3bc43444 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Tue, 5 Jan 2021 13:06:36 -0500 Subject: generalize --- shelf.hs | 60 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/shelf.hs b/shelf.hs index 954cf94..e5b615d 100755 --- a/shelf.hs +++ b/shelf.hs @@ -3,6 +3,7 @@ --ghc-options -Wall --ghc-options -Wno-unused-imports --ghc-options -Wno-name-shadowing -} {-# language NoImplicitPrelude #-} {-# language DuplicateRecordFields #-} +{-# language RecordWildCards #-} import Rebase.Prelude import Control.Lens @@ -30,28 +31,51 @@ formatFeet l = unwords $ toList (feet f) ++ toList (inches i n d) type Inches = Rational -boardLength, boardThickness, kickerHeight, availableSpace :: Inches -boardLength = 6 * 12 -boardThickness = 5/8 -kickerHeight = 4 + 1/2 -numHorizontals, numShelves :: Int -numShelves = length shelves +data Shelf = Shelf { + boardLength :: Inches, + boardThickness :: Inches, + kickerHeight :: Inches, + shelves :: [Integer] +} -numHorizontals = numShelves + 1 -availableSpace = boardLength - kickerHeight - (toRational numHorizontals) * boardThickness +bookshelf :: Shelf +bookshelf = Shelf { + boardLength = 6 * 12, + boardThickness = 5/8, + kickerHeight = 4 + 1/2, + shelves = [4, 4, 4, 3, 3] +} -shelves :: [Inches] -shelves = map (* availableSpace) $ map (% 18) [4, 4, 4, 3, 3] +pianoCubbies :: Shelf +pianoCubbies = Shelf { + boardLength = 52 + 1%8, + boardThickness = 5/8, + kickerHeight = 0, + shelves = [1, 1, 1, 1] +} -positions, positions' :: [Inches] -positions = map sum $ (tail . inits) $ concatMap (: [boardThickness]) $ kickerHeight : shelves +positions, positions' :: Shelf -> [Inches] +positions Shelf{..} = map sum $ (tail . inits) $ concatMap (: [boardThickness]) $ kickerHeight : shelvesAbs + where + shelvesAbs = map (* availableSpace) shelves' + shelves' = map (% (sum shelves)) shelves + numHorizontals, numShelves :: Int + numShelves = length shelves + numHorizontals = numShelves + 1 + availableSpace = boardLength - kickerHeight - (toRational numHorizontals) * boardThickness + +positions' x@Shelf{..} = map (boardLength -) $ positions x -positions' = map (boardLength -) positions +printPositions :: Shelf -> IO () +printPositions x = do + mapM_ putStrLn $ formatFeet <$> positions x + when (positions x /= reverse (positions' x)) $ do + putStrLn "\nReversed:\n" + mapM_ putStrLn $ formatFeet <$> positions' x main :: IO () main = do - -- assert $ sum shelves == 1 - mapM_ putStrLn $ formatFeet <$> positions - putStrLn "" - mapM_ putStrLn $ formatFeet <$> positions' - return () + putStrLn "Piano cubbies:\n" + printPositions pianoCubbies + putStrLn "\n\nBookshelf:\n" + printPositions bookshelf -- cgit v1.2.3