module Exercise_6 where import Data.Maybe import Data.Functor import Data.Bool import Data.Ratio import GHC.Real (Ratio( (:%) )) import Control.Monad {- isJust :: World -> Bool isJust _ = False -} {- Type signatures up here because that simplifies copy-paste into Tokenize. -} stepFractran :: [Rational] -> Integer -> Maybe Integer traceFractran' :: [Rational] -> Integer -> [Integer] {-WETT-} integerProduct :: Integer -> Rational -> Maybe Integer integerProduct x f = n <$ guard (d == 1) where n :% d = fromIntegral x * f traceFractran :: [Rational] -> Integer -> [Integer] traceFractran fs n = n : -- we were definitely able to reach this one ( concatMap (traceFractran fs) -- because: instance Foldable (Maybe a) $ msum -- first Just or Nothing $ integerProduct n <$> fs ) {-TTEW-} {-MCCOMMENT This was a really cool exercise, thanks for making me look up the monoid operations :) -} stepFractran fs n = msum $ integerProduct n <$> fs traceFractran' fs n = catMaybes $ takeWhile isJust $ iterate (>>= stepFractran fs) $ pure n