从一个长方形中剪下一个最大的正方形,剩下部分是一个新的长方形;从这个新的长方形中再次剪下一个最大的正方形,剩下部分依然是一个长方形;从这个新的长方形中第三次剪下一个最大的正方形,此时剩余部分是一个长为23cm,宽为20cm的长方形,则原长方形的周长是多少?
Solution by Josh
data Rect = Rect Int Int deriving Show
flatMap :: (a -> [b]) -> [a] -> [b]
flatMap _ [] = []
flatMap f (x:xs) = (f x) ++ flatMap f xs
expand :: Rect -> [Rect]
expand (Rect a b) = [(Rect (a + b) b), (Rect a (a + b))]
nExpand :: Int -> [Rect] -> [Rect]
nExpand 0 = id
nExpand n = nExpand (n - 1) . (flatMap expand)
perimeter :: Rect -> Int
perimeter (Rect a b) = 2 * (a + b)
main =
(print . (map perimeter) . (nExpand 3)) [(Rect 23 20)]