× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Understanding error in the insertion of an element in a list

Carsten König
Jul 17, 2015
<p>Your problem is (as Silvio already told you) the use of <code>:</code> but let's analyse the types to see where the error comes from and how you could see it yourself:</p> <h3>let's check the types</h3> <p>look at</p> <pre><code>(take k l : x) ++ drop k l </code></pre> <p><code>(++)</code> has type <code>[a] -&gt; [a] -&gt; [a]</code> so both parts <code>take k l : x</code> and <code>drop k l</code> need to be lists of the same type.</p> <p>looking at <code>drop</code> you get <code>Int -&gt; [a] -&gt; [a]</code> so you now know:</p> <ul> <li><code>k</code> has type <code>Int</code></li> <li><code>l</code> has type <code>[a]</code></li> <li>your result is the same type <code>[a]</code></li> <li><code>take k l : x</code> too needs to have type <code>[a]</code></li> </ul> <p>now look at <code>(:)</code> it has type <code>a -&gt; [a] -&gt; [a]</code> so for it to work out you now need <code>x</code> to have type <code>[a]</code> and <code>take k l</code> to have type <code>a</code>.</p> <p><code>take</code> has type <code>Int -&gt; [a] -&gt; [a]</code> and we already know that <code>l</code> has type <code>[a]</code> so you get:</p> <ul> <li><code>take k l</code> has type <code>[a]</code></li> <li>but you also need (see above at <code>(:)</code>) <code>take k l</code> to be of type <code>a</code></li> </ul> <p>So you need <code>a</code> to be somehow at the same time to be <code>[a]</code> that is what <code>a ~ [a]</code> means and is what the error says.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/29641345/Understanding%20error%20in%20the%20insertion%20of%20an%20element%20in%20a%20list/29641538">Stack Overflow</a>.</p>
comments powered by Disqus