Note that there are some explanatory texts on larger screens.

plurals
  1. POThe questions regarding my first haskell program
    primarykey
    data
    text
    <p>The program return all possible combinations of '0' and '1' length N.</p> <pre><code>addToElement :: String -&gt; String -&gt; String addToElement element symbol = element ++ symbol addOneToElement :: String -&gt; String addOneToElement element = addToElement element "1" addZeroToElement :: String -&gt; String addZeroToElement element = addToElement element "0" processListOnce :: [String] -&gt; [String] processListOnce lst = do let s1 = map addOneToElement lst let s2 = map addZeroToElement lst s1 ++ s2 processList :: [String] -&gt; Integer -&gt; [String] processList lst 1 = processListOnce lst processList lst n = do let tmp = processListOnce(lst) processList tmp (n - 1) {- processList2 :: [String] -&gt; Integer -&gt; [String] processList2 lst n = iterate (map processListOnce) lst !! n -} main = do let s = processList ["0", "1"] 2 let ss = show s putStrLn ss </code></pre> <p>It is my first Haskell program so I will be thankful if you help me:</p> <ul> <li><p>First of all pls refactore my code Haskell-way. I already know one magic refactring:</p> <pre><code> Control.Monad.replicateM n [0,1] </code></pre> <p>but this solution is not good for studying purposes :)</p></li> <li><p>Why I can not use ProcessList2 instead ProcessList and get error:</p> <pre><code>all_possible_combinations.hs:44:51: Couldn't match expected type `[Char]' against inferred type `Char' Expected type: [String]] Inferred type: [String] In the second argument of `iterate', namely `lst' In the first argument of `(!!)', namely `iterate (map processListOnce) lst' </code></pre> <ul> <li><p>Is there any way to skip (not use) the 'tmp' variable in the processList? I have tried, but got the error:</p> <pre><code>processList :: [String] -&gt; Integer -&gt; [String] processList lst 1 = processListOnce lst processList lst n = processList processListOnce(lst) (n - 1) all_possible_combinations.hs:39:32: Couldn't match expected type `[String]' against inferred type `[String] -&gt; [String]' In the first argument of `processList', namely `processListOnce' In the expression: processList processListOnce (lst) (n — 1) In the definition of `processList': processList lst n = processList processListOnce (lst) (n — 1) </code></pre></li> </ul></li> </ul> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload