In today’s Programming Praxis exercise, our goal is to generate the first 100 terms of a mathematical set defined by Niklaus Wirth. Let’s get started, shall we?
A quick import:
Thanks to Haskell’s lazy evaluation, we can simply define the set recursively and we don’t have to bother with a queue of items. Initially I just wrote out the union function (which merges two ascending lists in ascending order) my self, but I then I figured that there was probably a library somewhere that had this function. A bit of googling revealed Data.List.Ordered.
m :: [Integer] m = 1 : union (map ((+1) . (*2)) m) (map ((+1) . (*3)) m)
All that’s left to do is to take the first 100 elements of the sequence.
main :: IO () main = print $ take 100 m