## Partition An Integer n to an arithmetic series

Give a positive integer n, generate all partition of n to an arithmetic series with common difference 1.

First, a straightforward solution:

import Data.List (find) import Data.Maybe (catMaybes) -- partition positive integer n to an arithmetic series partitionToArithmetic0 n = catMaybes $ map partition [1..(n-2)] where partition x = let ys = [x..(n-1)] in case find (p x) ys of Just y -> Just (x, y) Nothing -> Nothing p x y | (x+y) * (y-x+1) == 2 *n = True | otherwise = False

Second, a not so straightforward solution, but much more effective than the first:

partitionToArithmetic1 n = catMaybes $ map partition [2..hh] where hh = floor $ sqrt $ fromIntegral $ 2*n partition h | not $ s > d = Nothing | not $ or [ hr /= 0 && nh == 0, hr == 0 && 2*nh == h] = Nothing | otherwise = Just (s-d, s+d1) where s = n `div` h d = (h-1) `div` 2 d1 = d + (if even h then 1 else 0) hr = h `rem` 2 nh = n `rem` h

You can check these two solutions are equal (upto n) by

test u = and $ map (\n -> (reverse $ partitionToArithmetic0 n) == partitionToArithmetic1 n) [1..u]

Exercise: Prove solution two is right.

Answer:

Let hight (or length) of an arithmetic series partition of a positive integer n is h, and

s = n/h - (n % h) d = floor( (h-1) / 2) d1 = d + (if even h then 1 else 0)

We’ll prove it by three steps.

Step 1, [s-d .. s+d1] is an arithmetic series partition of the positive integer n.

(s-d + s+d+[even h]) (s+d+[even h] - (s-d) + 1) / 2 = (2*s + [even h]) (2*d + [even h] + 1) /2 = (2*s + [even h]) * h / 2 = n - (n % h) + [even h] * h / 2 If [even h] * h = 2 * (n % h), we have sum [s-d .. s+d1] = n. ([even h] = if even h then 1 else 0)

Step 2, an arithmetic series partition of the positive integer n can be write as [s-d .. s+d1].

To prove this, we only need to prove that s = n/h – (n%h)/h.

(s-d + s+d+[even h]) (s+d+[even h] - (s-d) + 1) / 2 = n

so

s = n/h - [even h]/2

Because [even h] * h = 2 * (n % h), so we have s = n/h – (n % h)/h.

Step 3, h <= sqrt (2*n).

For partition n = h + (h-1) + … + 2 + 1, h get maximum value.

(2 + (h-1)) * h / 2 >= n

so

h * (h+1) >= 2*n

If h = sqrt (2*n), h * (h + 1) = 2*n + sqrt (2*n) > 2*n.

## Understanding Functions Which Use ‘instance Monad []’ by Equational Reasoning

GüŸnther Schmidt asked in Haskell-Cafe how to get a stream like this:

["a", ... , "z", "aa", ... , "az", "ba", ... , "bz", ... ]

and people in Haskell-Cafe offer some interesting answer for this question. On the one hand, these answers show the power of Haskell and GHC base libraries, but on the other hand, understanding them is a challenge for Haskell newbie like me. But I found to understand these answers, equational reasoning is very helpful, here is why I think so.

Answer 1 (by Matthew Brecknell):

concat $ tail $ iterate (map (:) ['a' .. 'z'] <*>) [[]]

Well, how does this expression do what we want? concat, tail, iterate, map, are easy, looks like the magic is in (<*>).

What’s this operator mean? (<*>) comes from class Applicative of Control.Applicative,

class Functor f => Applicative f where

— | Lift a value.

pure :: a -> f a

— | Sequential application.

(<*>) :: f (a -> b) -> f a -> f b

and ‘instance Applicative []’ is

instance Applicative [] where

pure = return

(<*>) = ap

ap comes from Control.Monad

ap :: (Monad m) => m (a -> b) -> m a -> m b

ap = liftM2 id

liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r

liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }

so the key to understand (<*>) is understanding the meaning of liftM2.

liftM2 uses, hum, do-notation, so according to Haskell 98 report, this can be translated to

liftM2 f m1 m2

= m1 >>= \x1 ->

m2 >>= \x2 ->

return (f x1 x2)

When it is applied to list (you can convince yourself of this by type inference), wee need ‘instance Monad []’

instance Monad [] where

m >>= k = foldr ((++) . k) [] m

m >> k = foldr ((++) . (\ _ -> k)) [] m

return x = [x]

fail _ = []

so

liftM2 f m1 m2

= m1 >>= \x1 ->

m2 >>= \x2 ->

return (f x1 x2)

let

f1

= \x1 ->

m2 >>= \x2 ->

return (f x1 x2)

f2

= \x2 -> return (f x1 x2)

we can write

m1 >>= f1

= foldr ((++) . f1) [] m1

m2 >>= f2

= foldr ((++) . f2) [] m2

Now we can see for list m1, m2, how does ‘liftM2 f m1 m2′ work

z1 = []

foreach x1 in (reverse m1); do — foldr ((++) . f1) [] m1

z2 = []

foreach x2 in (reverse m2); do — foldr ((++) . f2) [] m2

z2 = [f x1 x2] ++ z2

done

z1 = z2 ++ z1

done

Now we are ready to see how to apply (<*>):

map (:) ['a' .. 'z'] <*> [[]]

= (map (:) ['a' .. 'z']) <*> [[]]

= [('a':), ..., ('z':)] <*> [[]] — misuse of [...] notation

= ap [('a':), ..., ('z':)] [[]]

= liftM2 id [('a':), ..., ('z':)] [[]]

= [('a':), ..., ('z':)] >>= \x1 ->

[[]] >>= \x2 ->

return (id x1 x2)

Here x1 bind to (‘z':), …, (‘a':) in turn, x2 always bind to [], and noticed that

return (id (‘z':) []) — f = id; x1 = (‘a':); x2 = []

= return ((‘z':) [])

= return ((:) ‘z’ [])

= return "z"

= ["z"]

we have

map (:) ['a' .. 'z'] <*> [[]]

= liftM2 id [('a':), ..., ('z':)] [[]]

= ["a", ..., "z"]

(If you can’t follow the this, work through the definition of foldr step by step will be very helpful.)

map (:) ['a' .. 'z'] <*> (map (:) ['a' .. 'z'] <*> [[]])

= map (:) ['a' .. 'z'] <*> ["a", ..., "z"]

= liftM2 id [('a':), ..., ('z':)] ["a", ..., "z"]

= ["aa", ..., "az", "ba", ..., "bz", ..., "za", ..., "zz"]

Now it’s easy to know what we get from

iterate (map (:) ['a' .. 'z'] <*>) [[]]

= [[], f [[]], f (f [[]]), …] — f = map (:) ['a' .. 'z'] <*>

so

concat $ tail $ iterate (map (:) ['a' .. 'z'] <*>) [[]]

is exactly what we want.

Understanding Haskell codes by equational reasoning could be a very tedious process, but it’s also a very helpful and instructive process for the beginners, because it make you think slowly, check the computation process step by step, just like the compiler does. And in my opinion, this is exactly what a debugger does.

concatMap (\n -> replicateM n ['a'..'z']) [1..]

In this solution, the hardest part is replicatM, which come from Control.Monad

replicateM :: (Monad m) => Int -> m a -> m [a]

replicateM n x = sequence (replicate n x)

sequence :: Monad m => [m a] -> m [a]

sequence ms = foldr k (return []) ms

where

k m m’ = do { x <- m; xs <- m'; return (x:xs) }

recall the defintion of liftM2:

liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r

liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }

so k in definition of sequence is an application of liftM2, and sequence itself is a normal foldr.

Exercise:

Suppose n and xs have appropriate types, prove that

replicateM n xs = (iterate (map (:) xs <*>) [[]]) !! n

by equational reasoning.

Answer:

replicateM n xs = (iterate (map (:) xs <*>) [[]]) !! n

Definition of replicateM:

replicateM :: (Monad m) => Int -> m a -> m [a]

replicateM n x = sequence (replicate n x)

sequence :: Monad m => [m a] -> m [a]

sequence ms = foldr k (return []) ms

where

k m m’ = do { x <- m; xs <- m'; return (x:xs) }

Definition of (<*>):

instance Applicative [] where

pure = return

(<*>) = ap

ap :: Monad m => m (a -> b) -> m a -> m b

ap = liftM2 id

liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r

liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }

so

replicateM n xs

= sequence (replicate n xs)

= sequence [xs, ..., xs] — length [xs, ..., xs] == n

= foldr k (return []) [xs, ..., xs] — k = liftM2 (:)

= foldr k [[]] [xs, ..., xs]

= xs `k` (xs `f` … (xs `k` [[]]) …)

= k xs (k xs … (k xs [[]]) …)

= f (f … (f [[]])) — f = liftM2 (:) xs

for right hand, because

map (:) xs <*>

= ap (map (:) xs)

= liftM2 id (map (:) xs)

= liftM2 (:) xs

we have

(iterate (map (:) xs <*>) [[]]) !! n

= (iterate (liftM2 (:) xs) [[]]) !! n

= f (f … (f [[]])) — f = liftM2 (:) xs

Left hand and right hand are equal.

## Understanding ‘instance Monad ((->) r)’ by type inference

While reading source code of Control.Monad.Instances, I found I can’t understand ‘instance of Monad ((->) r)’, but after read Brent Yorgey’s reply in his blog post about The Typeclassopedia, he said, ‘the data constructor for (->) is called… lambda’, I suddenly found I CAN understand them by type inference. Here is how I do these.

There are two points:

- follow the types
- the data constructor for type constructor (->) is called lambda abstraction

This is the source code from Control.Monad.Instances:

instance Monad ((->) r) where return = const f >>= k = \r -> k (f r) r

The case of `return`

is trivial. Look at it’s type first: `return :: (Monad m) => a -> m a`

, so for ‘instance ((->) r)’, we have `m = (->) r`

then

a -> m a = a -> ((->) r a) = a -> r -> a

Now look at const’s type: `const :: a -> b -> a`

, exactly what we want. But what we got from ‘return a’? It is

return a = (->) r a -- misuse (->) at here = \r -> a -- data constructor of (->) is lambda = \_ -> a

this is exactly what we got from ‘const a’, i.e. `const a = \_ -> a`

`(>>=)`

is a bit complicate, but if you follow its type, you can understand it pretty easily.

Let’s look at it’s type first:

`(>>=) :: (Monad m) => m a -> (a -> m b) -> m b`

As before, for ‘instance ((->) r)’, we have ` m = (->) r`

, then

f :: m a = (->) r a k :: a -> m b = a -> ((->) r b) = a -> r -> b

and suppose

f r = a k a r = b

then we have

f >>= k = \r -> k (f r) r = \r -> k a r = \r -> b = (->) r b -- misuse of (->) = m b

Err..a bit misuse of (->) and didn’t distinguish type variables and normal variables, but it works for me. If you have the similar difficult to understanding ‘instance Monad ((->) r)’, I hope these work for you too.

## Finish reading of FP Implementation

Finish reading of The Implementation of Functional Programming Languages by Simon Peyton Jones.

After reading this very readable book, you will get a global conceptual understanding of implementation of functional programming languages.But because it’s published 22 years ago (1987), so it’s a little out of date now (for example, it didn’t include the newest implementation technics of functional programming language). Despit it’s age, everyone who interested in implementation of functional programming languages should read it.

## Finish reading of The Typeclassopedia

Finish reading of The Typeclassopedia from The Monad.Reader 13, but it’s just the beginning of a series of reading about papers, articles, wikibooks, blog posts, and codes, as the author said.

It’s a great article, talks about Haskell’s type class and their relationship, has plenty of very useful references.

## Sieve of Eratosthenes In Haskell

Algorithm description, see Sieve of Eratosthenes

module Main where import System.Environment isPrime p (x:xs) | x*x > p = True | p `mod` x == 0 = False | otherwise = isPrime p xs primes = 2 : oprimes where oprimes = 3 : [ p | p <- [5,7..], isPrime p oprimes] main = do args <- getArgs let n = read $ args !! 0 x = takeWhile (< n) primes print $ length x print x

There is a HackageDB package called primes which provides an efficient lazy wheel sieve for prime generation.

This version looks nicer, but less effective:

sieve (x:xs) = x : sieve xs' where xs' = filter (\y -> y `mod` x /= 0) xs primes = 2 : sieve [3,5..]