Programming Praxis – Wirth Problem 15.12

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
About these ads

Tags: , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: