
Hi - How can I generate all binary string of a given length? The type signature would something like - genbin :: Int -> [String] For example genbin 2 would give ["00","11","01","10"] and genbin 3 would give ["000","001","010","011","100","101","110","111"] etc.. thanks..

genbin = flip replicateM "01"
2010/10/15 rgowka1
Hi -
How can I generate all binary string of a given length? The type signature would something like -
genbin :: Int -> [String]
For example genbin 2 would give ["00","11","01","10"] and genbin 3 would give ["000","001","010","011","100","101","110","111"] etc..
thanks.. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Senior Software Engineer, Grid Dynamics http://www.griddynamics.com/

Here's why it works:
genbin 3 = replicateM 3 "01" = (unfold replicateM) do x1 <- "01"; x2
<- "01" ; x3 <- "01"; return [x1,x2,x3] = your desired result
(enumerate all combinations of x1,x2,x3 with each being 0 or 1).
2010/10/15 Eugene Kirpichov
genbin = flip replicateM "01"
2010/10/15 rgowka1
: Hi -
How can I generate all binary string of a given length? The type signature would something like -
genbin :: Int -> [String]
For example genbin 2 would give ["00","11","01","10"] and genbin 3 would give ["000","001","010","011","100","101","110","111"] etc..
thanks.. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Senior Software Engineer, Grid Dynamics http://www.griddynamics.com/
-- Eugene Kirpichov Senior Software Engineer, Grid Dynamics http://www.griddynamics.com/

Amazing, will never find this in any other languagw. But ghci crashes
for bigger input. Like genbin 20. How to scale this function?
On 10/15/10, Eugene Kirpichov
Here's why it works:
genbin 3 = replicateM 3 "01" = (unfold replicateM) do x1 <- "01"; x2 <- "01" ; x3 <- "01"; return [x1,x2,x3] = your desired result (enumerate all combinations of x1,x2,x3 with each being 0 or 1).
2010/10/15 Eugene Kirpichov
: genbin = flip replicateM "01"
2010/10/15 rgowka1
: Hi -
How can I generate all binary string of a given length? The type signature would something like -
genbin :: Int -> [String]
For example genbin 2 would give ["00","11","01","10"] and genbin 3 would give ["000","001","010","011","100","101","110","111"] etc..
thanks.. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Senior Software Engineer, Grid Dynamics http://www.griddynamics.com/
-- Eugene Kirpichov Senior Software Engineer, Grid Dynamics http://www.griddynamics.com/

On Fri, 15 Oct 2010 14:34:42 +0200, rgowka1
Amazing, will never find this in any other languagw. But ghci crashes for bigger input. Like genbin 20. How to scale this function?
Well, "scaling" this isn't really possible, because of its complexity. It generates all permutations of a given string with two states for each position. In regular languages, this is the language {1,0}^n, n being the length of the string. This means that there are 2^n different strings in the language. For 20, that's already 1048576 different Strings! Strings are furthermore not really the best way to encode your output. Numbers (i.e. bytes) would be much better. You could generate them, and only translate into strings when needed. HTH, Aleks

Actually my ghci doesn't crash for genbin 25 (haven't tried further),
though it eats quite a bit of memory.
How are you going to use these bit strings? Do you need all of them at once?
2010/10/15 Aleksandar Dimitrov
On Fri, 15 Oct 2010 14:34:42 +0200, rgowka1
wrote: Amazing, will never find this in any other languagw. But ghci crashes for bigger input. Like genbin 20. How to scale this function?
Well, "scaling" this isn't really possible, because of its complexity. It generates all permutations of a given string with two states for each position. In regular languages, this is the language {1,0}^n, n being the length of the string. This means that there are 2^n different strings in the language. For 20, that's already 1048576 different Strings! Strings are furthermore not really the best way to encode your output. Numbers (i.e. bytes) would be much better. You could generate them, and only translate into strings when needed.
HTH, Aleks _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Senior Software Engineer, Grid Dynamics http://www.griddynamics.com/

I expect this one to run in constant space: import Data.Bits genbin :: Int -> [String] genbin n = map (showFixed n) [0..2^n-1::Int] where showFixed n i = map (bool '1' '0' . testBit i) [n-1,n-2..0] bool t f b = if b then t else f Daniel On Oct 15, 2010, at 9:43 AM, Eugene Kirpichov wrote:
Actually my ghci doesn't crash for genbin 25 (haven't tried further), though it eats quite a bit of memory. How are you going to use these bit strings? Do you need all of them at once?
2010/10/15 Aleksandar Dimitrov
: On Fri, 15 Oct 2010 14:34:42 +0200, rgowka1
wrote: Amazing, will never find this in any other languagw. But ghci crashes for bigger input. Like genbin 20. How to scale this function?
Well, "scaling" this isn't really possible, because of its complexity. It generates all permutations of a given string with two states for each position. In regular languages, this is the language {1,0}^n, n being the length of the string. This means that there are 2^n different strings in the language. For 20, that's already 1048576 different Strings! Strings are furthermore not really the best way to encode your output. Numbers (i.e. bytes) would be much better. You could generate them, and only translate into strings when needed.
HTH, Aleks _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Senior Software Engineer, Grid Dynamics http://www.griddynamics.com/ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks Daniel.
But genbin 32 gives an empty list.. works till 31.
On Fri, Oct 15, 2010 at 9:05 AM, Daniel Gorín
I expect this one to run in constant space:
import Data.Bits
genbin :: Int -> [String] genbin n = map (showFixed n) [0..2^n-1::Int] where showFixed n i = map (bool '1' '0' . testBit i) [n-1,n-2..0] bool t f b = if b then t else f
Daniel
On Oct 15, 2010, at 9:43 AM, Eugene Kirpichov wrote:
Actually my ghci doesn't crash for genbin 25 (haven't tried further), though it eats quite a bit of memory. How are you going to use these bit strings? Do you need all of them at once?
2010/10/15 Aleksandar Dimitrov
: On Fri, 15 Oct 2010 14:34:42 +0200, rgowka1
wrote: Amazing, will never find this in any other languagw. But ghci crashes for bigger input. Like genbin 20. How to scale this function?
Well, "scaling" this isn't really possible, because of its complexity. It generates all permutations of a given string with two states for each position. In regular languages, this is the language {1,0}^n, n being the length of the string. This means that there are 2^n different strings in the language. For 20, that's already 1048576 different Strings! Strings are furthermore not really the best way to encode your output. Numbers (i.e. bytes) would be much better. You could generate them, and only translate into strings when needed.
HTH, Aleks _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Senior Software Engineer, Grid Dynamics http://www.griddynamics.com/ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, 15 Oct 2010 09:16:58 -0400, rgowka1
But genbin 32 gives an empty list.. works till 31.
That's because Daniel uses values of type Int as intermediate storage during the computation, and Int values are only 32 bits long. By replacing Int with Integer (which does not have that limitation), you can make it work for larger numbers/longer strings: genbin n = map (showFixed n) [0..2^n-1::Integer] -Steve Schafer

Not the most efficient, but easy to understand:
genbin 0 = []
genbin 1 = ["0", "1"]
genbin i =
map ('0' :) x ++ map ('1' :) x
where
x = genbin $ i - 1
On Fri, Oct 15, 2010 at 2:21 PM, rgowka1
Hi -
How can I generate all binary string of a given length? The type signature would something like -
genbin :: Int -> [String]
For example genbin 2 would give ["00","11","01","10"] and genbin 3 would give ["000","001","010","011","100","101","110","111"] etc..
thanks.. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (6)
-
Aleksandar Dimitrov
-
Daniel Gorín
-
Eugene Kirpichov
-
Michael Snoyman
-
rgowka1
-
Steve Schafer