Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's a lot of things you could possibly mean. For example, if you want a few copies of a single value, you can use <code>replicate</code>, defined in the Prelude:</p> <pre><code>replicate :: Int -&gt; a -&gt; [a] replicate 0 x = [] replicate n | n &lt; 0 = undefined | otherwise = x : replicate (n-1) x </code></pre> <p>In ghci:</p> <pre><code>Prelude&gt; replicate 4 ("Haskell", 2) [("Haskell",2),("Haskell",2),("Haskell",2),("Haskell",2)] </code></pre> <p>Alternately, perhaps you actually want to do some IO to determine the list. Then a simple loop will do:</p> <pre><code>getListFromUser = do putStrLn "keep going?" s &lt;- getLine case s of 'y':_ -&gt; do putStrLn "enter a value" v &lt;- readLn vs &lt;- getListFromUser return (v:vs) _ -&gt; return [] </code></pre> <p>In ghci:</p> <pre><code>*Main&gt; getListFromUser :: IO [(String, Int)] keep going? y enter a value ("Haskell",2) keep going? y enter a value ("Prolog",4) keep going? n [("Haskell",2),("Prolog",4)] </code></pre> <p>Of course, this is a particularly crappy user interface -- I'm sure you can come up with a dozen ways to improve it! But the pattern, at least, should shine through: you can use values like <code>[]</code> and functions like <code>:</code> to construct lists. There are many, many other higher-level functions for constructing and manipulating lists, as well.</p> <p>P.S. There's nothing particularly special about lists of tuples (as compared to lists of other things); the above functions display that by never mentioning them. =)</p>
 

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