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:

import Data.List.Ordered

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

### Like this:

Like Loading...

*Related*

Tags: bonsai, code, Haskell, kata, niklaus, praxis, programming, wirth

This entry was posted on December 7, 2012 at 10:48 am and is filed under Programming Praxis. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

## Leave a Reply