<div dir="ltr">Hello fellow Haskellers!<br><div><br>I'm approaching learning the basics of Haskell by going through <a href="https://wiki.haskell.org/99_questions" target="_blank">https://wiki.haskell.org/99_<wbr>questions</a>. At the same time I write tests for my code in Hspec. Consider question no. 2: "Find the last but one element of a list". <br><br>My solution:<br><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:"Droid Sans Mono","monospace",monospace,"Droid Sans Fallback";font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(96,139,78)">-- Problem 02</span></div><div><span style="color:rgb(220,220,170)">myButLast</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">::</span><span style="color:rgb(212,212,212)"> [</span><span style="color:rgb(156,220,254)">a</span><span style="color:rgb(212,212,212)">] </span><span style="color:rgb(86,156,214)">-></span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">a</span></div><div><span style="color:rgb(212,212,212)">myButLast </span><span style="color:rgb(86,156,214)">[]</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> error </span><span style="color:rgb(206,145,120)">"Empty list"</span></div><div><span style="color:rgb(212,212,212)">myButLast [x] </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> error </span><span style="color:rgb(206,145,120)">"List has only one element"</span></div><div><span style="color:rgb(212,212,212)">myButLast [x1,x2] </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> x1</span></div><div><span style="color:rgb(212,212,212)">myButLast (x</span><span style="color:rgb(212,212,212)">:</span><span style="color:rgb(212,212,212)">xs) </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> myButLast xs</span></div></div><br>and a a test:<br><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:"Droid Sans Mono","monospace",monospace,"Droid Sans Fallback";font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(212,212,212)"> describe </span><span style="color:rgb(206,145,120)">"02 myButLast"</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(212,212,212)">$</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">do</span></div><div><span style="color:rgb(212,212,212)"> it </span><span style="color:rgb(206,145,120)">"returns the last but one element of a list"</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(212,212,212)">$</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">do</span></div><div><span style="color:rgb(212,212,212)"> myButLast </span><span style="color:rgb(86,156,214)">[]</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(212,212,212)">`shouldThrow`</span><span style="color:rgb(212,212,212)"> anyErrorCall</span></div><div><span style="color:rgb(212,212,212)"> myButLast [</span><span style="color:rgb(181,206,168)">1</span><span style="color:rgb(212,212,212)">] </span><span style="color:rgb(212,212,212)">`shouldThrow`</span><span style="color:rgb(212,212,212)"> anyErrorCall <span style="background-color:rgb(56,118,29)">-- <- this line causes the problem</span></span></div><div><span style="color:rgb(212,212,212)"> myButLast [</span><span style="color:rgb(181,206,168)">1</span><span style="color:rgb(212,212,212)">..</span><span style="color:rgb(181,206,168)">4</span><span style="color:rgb(212,212,212)">] </span><span style="color:rgb(212,212,212)">`shouldBe`</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(181,206,168)">3</span></div><div><span style="color:rgb(212,212,212)"> myButLast [</span><span style="color:rgb(206,145,120)">'x'</span><span style="color:rgb(212,212,212)">,</span><span style="color:rgb(206,145,120)">'y'</span><span style="color:rgb(212,212,212)">,</span><span style="color:rgb(206,145,120)">'z'</span><span style="color:rgb(212,212,212)">] </span><span style="color:rgb(212,212,212)">`shouldBe`</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(206,145,120)">'y'</span></div><div><span style="color:rgb(212,212,212)"> myButLast </span><span style="color:rgb(206,145,120)">"abc"</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(212,212,212)">`shouldBe`</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(206,145,120)">'b'</span></div></div><div><br><br></div><div>Building tests with stack test command causes the below compilation error:<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> • No instance for (Num (IO a0)) arising from the literal ‘1’<br> • In the expression: 1<br> In the first argument of ‘myButLast’, namely ‘[1]’<br> In the first argument of ‘shouldThrow’, namely ‘myButLast [1]’<br> |<br>27 | myButLast [1] `shouldThrow` anyErrorCall<br> | <br></blockquote><br><br></div><div>From
what I understand, type of myButLast [1] is different than expected by
shouldThrow. What I don't understand is why exactly it behaves so and
how to fix this problem. Only that one assertion doesn't compile. The
others are fine. Particularly, why does <span style="background-color:rgb(0,0,0)"><span style="color:rgb(212,212,212)">myButLast </span><span style="color:rgb(86,156,214)">[]</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(212,212,212)">`shouldThrow`</span><span style="color:rgb(212,212,212)"> anyErrorCall </span></span> work but with one element it doesn't?<br><br></div><div>Can you please give me a hand?<div class="gmail-adL"><br></div></div></div></div>