module Exercise_6 where import Data.Ratio import Data.List import Data.Maybe import Control.Monad import Control.Applicative traceFractran :: [Rational] -> Integer -> [Integer] h :: Integer -> Rational -> Rational {-WETT-} -- Help Function: Multiply an Integer a with a Rational and return a rational h = (*) . toRational -- unfoldr is a function, which gets an initial seed value n and builds a list of intermediate n's by retrieving a tuple of -- type Maybe (n, next) where n is the value which will be appended to the list and next is the value which will be used in -- the next iteration. unfoldr stops at the first encounter of Nothing. the first part in the lambda basically is the function -- to which the result of 'find ((==1) . denominator . h x) rs' will be fed into, unless it is not Nothing, else the infix -- operator <$> will not create the tuple Just (next, next). -- In a nutshell if a value inside the list rs has been found where the value times the current x is an Integer, then it will -- be first transformed to an Integer and then a tuple (a, a) created with the join (,) function, where a is the next value x -- -- a disatvantage is that unfoldr will not add the initial n to the list, which is why it is appended traceFractran rs n = n : unfoldr (\x -> join (,) . toInteger . numerator . h x <$> find ((==1) . denominator . h x) rs) n {-TTEW-} {- OLD -- The iterate function generates the list of n's, where the 'mapper' function first try's to find the first occurence -- of an integer x, where x = n * r | r element of rs. If no value could be found then it returns 0, else x. traceFractran rs = takeWhile (/=0) . iterate (\x -> maybe 0 (toInteger . numerator . h x) $ find ((==1) . denominator . h x) rs) -}