[Haskell-cafe] Flagstone problem

Brent Yorgey byorgey at seas.upenn.edu
Sat Nov 6 07:03:13 EDT 2010


On Thu, Nov 04, 2010 at 10:50:06AM -0700, michael rice wrote:
> Hi,
> 
> I've been looking at a "flagstone problem," where no two adjacent
> n-tuples can be identical. I solved the problem with Icon using

Interesting stuff!

> 
> ========= Here's my Haskell code ========
> 
> import Data.Bits
> import Data.List
> 
> flagstone n =  foldl (++) "" (take n (map show (f $ group [if even y
> then 0 else 1 | y <- [bitcount x  | x <- [20..]]])))

By the way, I would write this as

flagstone n = concat . take n . map show . f . group 
            . map (fromEnum . odd . bitcount) $ [20..]

You should never use foldl (++) as it is rather inefficient: you get
things like (((a ++ b) ++ c) ++ d) ... which ends up traversing the
left part of the list repeatedly.  And list comprehensions can be nice
at times, but personally using map seems clearer in this case.

> ========= My question ========
> 
> A further exercise in the text:
> 
> "Exercise 5.-(a) Define a(n) as the sum of the binary
> digits in the binary representation of n. Define b(i) as
> the number of a's between successive zeros as before.
> Then T = b(1) b(2) b(3) b(4) ... gives an infinite
> sequence of *seven* symbols with no repeats. (b) Write
> a routine to generate a sequence for seven colors of
> beads on a string with no repeats."
> 
> I may be misreading, but does this make any sense?

Doesn't make much sense to me.  The sum of binary digits in the binary
representation of n will not be zero very often... 

-Brent


More information about the Haskell-Cafe mailing list