**Paste:**#107655**Author(s):**1HaskellADay**Language:**Haskell**Channel:**-**Created:**2014-07-17 11:53:40 UTC**Revisions:**- 2014-07-17 17:32:22 UTC #107666 (diff): No title (1HaskellADay)
- 2014-07-17 11:53:40 UTC #107655: Streams? BONUS! (1HaskellADay)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | module Data.Stream where infixr 5 :< {-- --------- LET'S GET THIS PARTY STARTED! --------------------------------- So I wrote all that (rhetoric) about comonads so I could write this: the REAL DEAL. (translation: or, I just thought of this, just now, after writing all that comonadic stuff) So, one way to have yourself a stream is to do an (infinite) unfold or repeat something. Sure. Fine. That works. ... But not for me. Given the data type --} data Stream a = a :< Stream a {-- First of all, write some reasonable show function on it, so it doesn't go on forever showing you the result (translation: make Stream a a Show instance) instance Show a => Show (Stream a) where ... THEN make Stream a Functor instance Functor Stream where ... THEN make the Functor Stream a Comonad instance Comonad Stream where ... NOW we can get this party started If we have the stream of ones as: ones :: Stream Integer ones = 1 :< ones And the stream of nats as: nats :: Stream Integer nats = 0 :< (nats =>> succ . extract) (see how easy that was?) 1. define the fibonacci series as a stream : --} fibs :: Stream Integer fibs = undefined {-- Then define the function 'one' as the stream of 1/2, 1/4, ... And you can sum that to get (eventually) 1. No, wait. You need take and drop for that: --} takeS :: Int -> Stream a -> [a] takeS n stream = undefined dropS :: Int -> Stream a -> Stream a dropS n stream = undefined one :: Stream Double one = undefined -- How about 1/3? How would you define that as a stream? -- What other streams can you come up with? -- How about primes-...ish numbers off the 2-4-2 seive as a Stream? primesish :: Stream Integer primesish = undefined -- Streams are natural, Streams are fun, streams are best when they're ... -- ... I won't finish that line. -- ... I'll go now. -- Solution posted at http://lpaste.net/107665 |