module Sol10 where import HL10COR -- | 10.1 Write a corecursive definition that generates the even natural numbers. -- >>> take 5 evens -- [0,2,4,6,8] evens = 0 : map (+ 2) evens -- | 10.2 Use iterate to define the infinite stream of even natural numbers. -- >>> take 5 theEvens -- [0,2,4,6,8] theEvens = iterate (+ 2) 0 -- | 10.3 Thue-Morse sequence -- >>> take 100 thue -- "0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110" swap "" = "" swap ('1' : xs) = '0' : swap xs swap ('0' : xs) = '1' : swap xs morse xs = xs ++ morse (xs ++ swap xs) thue = '0' : morse "1" -- | 10.5 How would you implement a generator for streams of 0’s and 1’s with, in the long run, a proportion of 0’s and 1’s of 2 to 1? -- >>> random001s :: Int -> [Int] random001s i = map (`mod` 2) (randomInts 2 i) -- | 10.10 Vending machine from text -- >>> vend, vend1, vend2, vend3, vend4 :: Process vend (0 : xs) = "coin" : vend1 xs vend (1 : xs) = "coin" : vend4 xs vend1 (0 : xs) = "coin" : vend2 xs vend1 (1 : xs) = vend1 xs vend2 (0 : xs) = "beer" : vend xs vend2 (1 : xs) = "coin" : vend3 xs vend3 (0 : xs) = "moneyback" : vend xs vend3 (1 : xs) = vend3 xs vend4 (0 : xs) = "water" : vend xs vend4 (1 : xs) = vend4 xs