Skip to main content
deleted 181 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

How could this Haskell one-liner be shortened? Generating alphanumeric combinations

 
for i in {a..z}{a..z}{a..z}; do ...

So I decided to write it in Haskell:


So I decided to write it in Haskell,

    import Data.List
    c 0 _ = [[]]
    c n xs = [ y:ys | y:xs' <- Data.List.tails xs, ys <- c (n-1) xs']
    main = sequence_ . map putStrLn . c 3 $ ['a' .. 'z'] ++ ['0' .. '9']
 
Now, I couldn't find a way to incorporate the above code in my shell script, so I had to reduce it to an one-liner to run with `ghc -e`, finally giving me with the code I have right now

    

Now, I couldn't find a way to incorporate the above code in my shell script, so I had to reduce it to a one-liner to run with ghc -e, finally giving me the code I have right now:

for i in $(ghc -e "
      let c n l = if n == 0 then [[]] else \
        [y:s | y:q <- Data.List.tails l, s <- c (n-1) q] in
          sequence_. map putStrLn . c 3 $ ['a'..'z'] ++ ['0'..'9']"); do ...

Now, I actually like this style (elsewhere I have inline calls to awk too, etc). But I think this code is too big and too ugly; for example, I'm using `if n == 0` instead of guards, etc. Haskell is supposed to be a lot more concise than that.

Now, I'm not sure if this is the right forum to ask this question (but I'm not sure if there's a forum that is a *better* fit), but how can I make it more concise?

My only requirement is that I may want to change the length to be larger than 3, but also `['a'..'z'] ++ ['0'..'9']` to another set (perhaps to include - or _).

Now, I actually like this style (elsewhere I have inline calls to awk too, etc). But I think this code is too big and too ugly; for example, I'm using if n == 0 instead of guards, etc. Haskell is supposed to be a lot more concise than that.

My only requirement is that I may want to change the length to be larger than 3, but also ['a'..'z'] ++ ['0'..'9'] to another set (perhaps to include - or _).

How could this Haskell one-liner be shortened?

 

So I decided to write it in Haskell,

    import Data.List
    c 0 _ = [[]]
    c n xs = [ y:ys | y:xs' <- Data.List.tails xs, ys <- c (n-1) xs']
    main = sequence_ . map putStrLn . c 3 $ ['a' .. 'z'] ++ ['0' .. '9']
 
Now, I couldn't find a way to incorporate the above code in my shell script, so I had to reduce it to an one-liner to run with `ghc -e`, finally giving me with the code I have right now

    for i in $(ghc -e "
      let c n l = if n == 0 then [[]] else \
        [y:s | y:q <- Data.List.tails l, s <- c (n-1) q] in
          sequence_. map putStrLn . c 3 $ ['a'..'z'] ++ ['0'..'9']"); do ...

Now, I actually like this style (elsewhere I have inline calls to awk too, etc). But I think this code is too big and too ugly; for example, I'm using `if n == 0` instead of guards, etc. Haskell is supposed to be a lot more concise than that.

Now, I'm not sure if this is the right forum to ask this question (but I'm not sure if there's a forum that is a *better* fit), but how can I make it more concise?

My only requirement is that I may want to change the length to be larger than 3, but also `['a'..'z'] ++ ['0'..'9']` to another set (perhaps to include - or _).

Generating alphanumeric combinations

for i in {a..z}{a..z}{a..z}; do ...

So I decided to write it in Haskell:

import Data.List
c 0 _ = [[]]
c n xs = [ y:ys | y:xs' <- Data.List.tails xs, ys <- c (n-1) xs']
main = sequence_ . map putStrLn . c 3 $ ['a' .. 'z'] ++ ['0' .. '9']

Now, I couldn't find a way to incorporate the above code in my shell script, so I had to reduce it to a one-liner to run with ghc -e, finally giving me the code I have right now:

for i in $(ghc -e "
  let c n l = if n == 0 then [[]] else \
    [y:s | y:q <- Data.List.tails l, s <- c (n-1) q] in
      sequence_. map putStrLn . c 3 $ ['a'..'z'] ++ ['0'..'9']"); do ...

Now, I actually like this style (elsewhere I have inline calls to awk too, etc). But I think this code is too big and too ugly; for example, I'm using if n == 0 instead of guards, etc. Haskell is supposed to be a lot more concise than that.

My only requirement is that I may want to change the length to be larger than 3, but also ['a'..'z'] ++ ['0'..'9'] to another set (perhaps to include - or _).

edited title
Link
darque
  • 133
  • 3

Help with shortening How could this Haskell combinations snippedone-liner be shortened?

Post Migrated Here from codegolf.stackexchange.com (revisions)
Source Link
darque
  • 133
  • 3

Help with shortening this Haskell combinations snipped

I was happy with my shell script that needed to generate alphanumeric combinations of length N (in this case, 3)

Now I became less happy when I needed alphanumeric combinations (specially if I choose a larger value for N)

for i in {0..9}{a..z}{a..z} \
         {a..z}{0..9}{a..z} \
         {a..z}{a..z}{0..9} \
         {a..z}{a..z}{a..z} \
         {0..9}{0..9}{a..z} \
         {0..9}{a..z}{0..9} \
         {a..z}{0..9}{0..9} \
         {0..9}{0..9}{0..9}; do ...

So I decided to write it in Haskell,

    import Data.List
    c 0 _ = [[]]
    c n xs = [ y:ys | y:xs' <- Data.List.tails xs, ys <- c (n-1) xs']
    main = sequence_ . map putStrLn . c 3 $ ['a' .. 'z'] ++ ['0' .. '9']

Now, I couldn't find a way to incorporate the above code in my shell script, so I had to reduce it to an one-liner to run with `ghc -e`, finally giving me with the code I have right now

    for i in $(ghc -e "
      let c n l = if n == 0 then [[]] else \
        [y:s | y:q <- Data.List.tails l, s <- c (n-1) q] in
          sequence_. map putStrLn . c 3 $ ['a'..'z'] ++ ['0'..'9']"); do ...

Now, I actually like this style (elsewhere I have inline calls to awk too, etc). But I think this code is too big and too ugly; for example, I'm using `if n == 0` instead of guards, etc. Haskell is supposed to be a lot more concise than that.

Now, I'm not sure if this is the right forum to ask this question (but I'm not sure if there's a forum that is a *better* fit), but how can I make it more concise?

My only requirement is that I may want to change the length to be larger than 3, but also `['a'..'z'] ++ ['0'..'9']` to another set (perhaps to include - or _).