[Haskell-cafe] Flagstone problem
nowgate at yahoo.com
Sat Nov 6 10:49:38 EDT 2010
Efficiency aside, your code is definitely more readable. I flubbed that step from True/False to 0/1 using fromEnum. Haskell's grab bag has so many tools it's easy to omit one.
--- On Sat, 11/6/10, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
From: Brent Yorgey <byorgey at seas.upenn.edu>
Subject: Re: [Haskell-cafe] Flagstone problem
To: haskell-cafe at haskell.org
Date: Saturday, November 6, 2010, 7:03 AM
On Thu, Nov 04, 2010 at 10:50:06AM -0700, michael rice wrote:
> I've been looking at a "flagstone problem," where no two adjacent
> n-tuples can be identical. I solved the problem with Icon using
> ========= 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...
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe