I implemented the following function:
the ruler function
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, . . .
where the nth element in the stream (assuming the first element corresponds to n = 1) is the largest power of 2 which evenly divides n
I'm giving it a vague name in the event that students try to cheat and search for it.
foo :: Stream Integer foo = streamMap f startAtOne where f x = if (odd x) then 0 else g x 0 g y acc = if y == 0 then acc else g (y-2) (acc+1) startAtOne = streamFromSeed 1 (+1)
streamFromSeed function and the
Stream data type.
data Stream a = Stream (a, Stream a)
streamMap :: (a -> b) -> Stream a -> Stream b streamMap f (Stream (x, rest)) = Stream (f x, streamMap f rest) -- Given a start value and function, generate a stream by applying fn to each Stream value streamFromSeed :: a -> (a -> a) -> Stream a streamFromSeed x f = Stream $ (x, streamFromSeed (f x) f)
Note that this technically is homework, but from 2013. I'm working on this material since Prof. Yorgey provides excellent lectures and homework assignments. I'm not in this class nor a student at the school.