Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry, you can't<sup>1</sup>. There are fundamental differences between tuples and lists:</p> <ul> <li>A tuple always have a finite amount of elements, that is known <em>at compile time</em>. Tuples with different amounts of elements are actually different types.</li> <li>List an have as many elements as they want. The amount of elements in a list doesn't need to be known at compile time.</li> <li>A tuple can have elements of arbitrary types. Since the way you can use tuples always ensures that there is no type mismatch, this is safe.</li> <li>On the other hand, all elements of a list have to have the same type. Haskell is a statically-typed language; that basically means that all types are known at compile time.</li> </ul> <p>Because of these reasons, you can't. If it's not known, how many elements will fit into the tuple, you can't give it a type.</p> <p>I guess that the input you get from your user is actually a string like <code>"(1,2,3)"</code>. Try to make this directly a list, whithout making it a tuple before. You can use pattern matching for this, but here is a slightly sneaky approach. I just remove the opening and closing paranthesis from the string and replace them with brackets -- and voila it becomes a list.</p> <pre><code>tuplishToList :: String -&gt; [Int] tuplishToList str = read ('[' : tail (init str) ++ "]") </code></pre> <h2>Edit</h2> <p>Sorry, I did not see your latest comment. What you try to do is not that difficult. I use these simple functions for my task:</p> <ul> <li><p><code>words str</code> splits <code>str</code> into a list of words that where separated by whitespace before. The output is a list of <code>String</code>s. <strong>Caution</strong>: This only works if the string inside your tuple contains no whitespace. Implementing a better solution is left as an excercise to the reader.</p></li> <li><p><code>map f lst</code> applies <code>f</code> to each element of <code>lst</code></p></li> <li><p><code>read</code> is a magic function that makes a a data type from a String. It only works if you know before, what the output is supposed to be. If you really want to understand how that works, consider implementing <code>read</code> for your specific usecase.</p></li> </ul> <p>And here you go:</p> <pre><code>tuplish2List :: String -&gt; [(String,Int)] tuplish2List str = map read (words str) </code></pre> <hr> <p><sup>1</sup> As some others may point out, it may be possible using templates and other hacks, but I don't consider that a real solution.</p>
    singulars
    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.
    1. VO
      singulars
      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