-1

ı have a question. ı want to generate binary list .but between members of the list will be only one bit change.

oneBitAll :: Integral a => a -> [[String]]

for n=2

Output:

["00","01","11","10"] ve ["00","10","11","01"]

n=3

oneBitAll 3
[["000","001","011","010","110","111","101","100"], ["000","001","011","111","101","100","110","010"], ["000","001","101","100","110","111","011","010"], ["000","001","101","111","011","010","110","100"], ["000","010","011","001","101","111","110","100"], .....]

only one bit change between members.

please help.

this gives only one

g 0 = [""]
g n = (map ('0':)) (g (n-1)) ++ (map ('1':)) (reverse (g (n-1)))

gray code is true for this.but ı want to find all combinations.

how can I generate all possible gray codes for given n number?

permute [] = [[]]
permute xs = concatMap (\x -> map (x:) $ permute $ delete x xs) xs 
g 0 = [""]
g n = (map ('0':)) (g (n-1)) ++ (map ('1':)) (reverse (g (n-1)))
oneBitAll n = (map transpose . permute . transpose $ g n) 

This code generate half of possibilities.What can ı add this code?this code generates;

[["000","001","011","010","110","111","101","100"],["000","010","011","001","101","111","110","100"],["000","001","101","100","110","111","011","010"],["000","010","110","100","101","111","011","001"],["000","100","101","001","011","111","110","010"],["000","100","110","010","011","111","101","001"]]

but must generate 12 members.

Morwenn
  • 21,684
  • 12
  • 93
  • 152
rooney
  • 3
  • 4
  • hard problem?how can ı do? – rooney May 27 '16 at 15:56
  • google "Gray code" – ErikR May 27 '16 at 16:04
  • thanks but this will give all possible? – rooney May 27 '16 at 16:14
  • I think it is clear what he's asking, though it takes a bit of thought to read through the obvious English-as-a-second-language problems: how would one go about enumerating all gray codes of a given length? (Or, equivalently, how would one go about enumerating all Hamiltonian cycles on an n-dimensional cube?) So I do not vote to close as "unclear what you're asking", as others have. – Daniel Wagner May 27 '16 at 16:25
  • well - the OP edited the question just a few minutes ago and added his solution - the function `g` which I didn't see when I made my comment. – ErikR May 27 '16 at 16:30
  • thanks.one bit change like this.sequent members of list will have only one bit change.for example 001-011 only second bit different.011-010 only third bit different.sequent members will be like this.sorry for my english. – rooney May 27 '16 at 16:32
  • yes ı editted but this code gives only one posibilty.other possibilities werent found . – rooney May 27 '16 at 16:34
  • @ErikR I, too, read the answer before the edit, and even wrote up an answer with almost identical code before I realized what the question was about (which I then deleted -- all of this before I saw the edit). So while the question was perhaps subtle or hard to understand from the beginning, I do think all the interesting information was there even before the edit. – Daniel Wagner May 27 '16 at 16:38
  • can you help me finding all possibilities using this rules? – rooney May 27 '16 at 16:41
  • @rooney - this clearly smells like a homework problem. So what kinds of things have you covered in the course which might be relevant to this problem? Can you (in any computer language) write a program to find all possible paths through a maze? – ErikR May 27 '16 at 18:33
  • yes this is final examination question and ı m new in haskell .ı can write in c# but this wasnt accepted. – rooney May 27 '16 at 18:39
  • can you give me a tip if you know – rooney May 27 '16 at 18:39

1 Answers1

0

There is probably a smarter way to do this that exploits more of the structure of gray codes. This way is sort of quick and dirty, but it seems to work fairly well.

The basic idea is we'll generate all sequences of bitstrings, then filter out the ones that aren't gray codes. We'll be slightly more clever, though, in that we'll check prefixes of each sequence to make sure they could plausibly be extended to a gray code, and prune prefixes that can't be.

For our purposes, a gray code will have five properties:

  • Each pair of consecutive bitstrings differs in exactly one place.
  • The sequence is cyclic: the first and last bitstring also differ in exactly one place.
  • No two bitstrings in a sequence are equal.
  • A code with bitstring length n has 2^n elements.
  • To break the cyclic symmetry, every code will start with the all-zero bitstring.

Three of these properties can be expressed on code prefixes:

import Control.Monad
import Data.List

validCodePrefix xss = nearbyPairs && unique && endsWithZeros where
    nearbyPairs = all (uncurry nearby) (zip xss (tail xss))
    unique = all ((1==) . length) . group . sort $ xss
    endsWithZeros = all (all (=='0')) (take 1 (reverse xss))

nearby xs xs' = length [() | (x, x') <- zip xs xs', x /= x'] == 1

The cyclic condition applies only to completed codes, and can be written as:

cyclic xss = nearby (head xss) (last xss)

We can implement the search and enforce the length condition at the same time, by repeatedly choosing from all appropriate length bitstrings, and keeping only those ones that are valid:

codes n = go (2^n) [] where
    go 0 code = [reverse code | cyclic code]
    go i code = do
        continuation <- replicateM n "01"
        guard (validCodePrefix (continuation:code))
        go (i-1) (continuation:code)
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380