Skip to content

Instantly share code, notes, and snippets.

@NorfairKing
Created December 12, 2023 20:05
Show Gist options
  • Save NorfairKing/b95892fec7c206dfe614de51a74a4095 to your computer and use it in GitHub Desktop.
Save NorfairKing/b95892fec7c206dfe614de51a74a4095 to your computer and use it in GitHub Desktop.
-- V a b == a ± b
--
-- For example:
-- V 100 2 = 100 mm ± 2mm
data V = V
{ -- Middle value
value :: Double,
-- Must be positive
tollerance :: Double
}
deriving (Show)
maximal :: V -> Double
maximal (V v t) = v + t
minimal :: V -> Double
minimal (V v t) = v - t
addV :: V -> V -> V
addV (V v1 t1) (V v2 t2) = V (v1 + v2) (t1 + t2)
-- FIXME: This doesn't work for negative numbers
maxV :: V -> V -> V
maxV va@(V v1 t1) vb@(V v2 t2) =
let v = max v1 v2
mx = max (maximal va) (maximal vb)
in V v (mx - v)
data Fits
= DefinitelyNot
| Perhaps
| Definitely
deriving (Show)
fitsIn :: V -> V -> Fits
fitsIn v1 v2 =
if maximal v1 < minimal v2
then Definitely
else
if minimal v1 > maximal v2
then DefinitelyNot
else Perhaps
data Box = Box {height :: V, width :: V}
deriving (Show)
box1 :: Box
box1 = Box (V 50 2) (V 50 2)
box2 :: Box
box2 = box1
availableWidth :: V
availableWidth = V 100 2
nextTo :: Box -> Box -> Box
nextTo (Box h1 w1) (Box h2 w2) = Box (maxV h1 h2) (addV w1 w2)
fitsInTruck :: Box -> V -> Fits
fitsInTruck (Box _ w) v = w `fitsIn` v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment