<div dir="ltr">According to my guess:<div><ol><li><a href="https://www.stackage.org/haddock/lts-11.17/hspec-expectations-0.8.2/Test-Hspec-Expectations.html#v:shouldThrow">shouldThrow receives `IO a` as a first argument.</a></li><li>So, both ` <span style="color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30);text-decoration-style:initial;text-decoration-color:initial">myButLast </span><span style="font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30);text-decoration-style:initial;text-decoration-color:initial;color:rgb(86,156,214)">[]</span> ` and ` <span style="color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30);text-decoration-style:initial;text-decoration-color:initial">myButLast [</span><span style="font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30);text-decoration-style:initial;text-decoration-color:initial;color:rgb(181,206,168)">1</span><span style="color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30);text-decoration-style:initial;text-decoration-color:initial">]</span> ` must be some `IO a`</li><li>Then, <span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">`<span> </span></span><span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30)">myButLast</span><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">`</span> 's type is inferred as `[IO a] -> IO a` when given `[]`: the element of the list is `IO a` (Which actually would be `IO ()` by the default rule).</li><li>Similarly, <span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">`<span> </span></span><span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30)">myButLast [</span><span style="text-decoration-style:initial;text-decoration-color:initial;font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30);color:rgb(181,206,168)">1</span><span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30)">]</span><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"> `</span> 's `a` (the type of the element of the list) must be `IO something`. But any of `IO something` can't be a `Num`'s instance (as the error message " <span style="font-size:small;text-align:start;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">No instance for (Num (IO a0)) arising from the literal ‘1’</span> " says).</li></ol><div>That's why <span style="font-size:small;text-align:left;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline">`<span> </span></span><span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;white-space:pre-wrap;background-color:rgb(30,30,30)">myButLast [</span><span style="text-decoration-style:initial;text-decoration-color:initial;font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;white-space:pre-wrap;background-color:rgb(30,30,30);color:rgb(181,206,168)">1</span><span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;white-space:pre-wrap;background-color:rgb(30,30,30)">]</span><span style="font-size:small;text-align:left;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"> <span class="gmail-gr_ gmail-gr_197 gmail-gr-alert gmail-gr_spell gmail-gr_inline_cards gmail-gr_run_anim gmail-ContextualSpelling gmail-ins-del gmail-multiReplace" id="gmail-197" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">` can't be compiled.</span></span></div></div><div><span style="font-size:small;text-align:left;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><span class="gmail-gr_ gmail-gr_197 gmail-gr-alert gmail-gr_spell gmail-gr_inline_cards gmail-gr_run_anim gmail-ContextualSpelling gmail-ins-del gmail-multiReplace" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit"><br></span></span></div><div><span style="font-size:small;text-align:left;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><span class="gmail-gr_ gmail-gr_197 gmail-gr-alert gmail-gr_spell gmail-gr_inline_cards gmail-gr_run_anim gmail-ContextualSpelling gmail-ins-del gmail-multiReplace" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">To fix it, use `<a href="https://www.stackage.org/haddock/lts-11.17/base-4.10.1.0/Control-Exception.html#v:evaluate">evaluate</a>`:</span></span></div><div><span style="font-size:small;text-align:left;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><span class="gmail-gr_ gmail-gr_197 gmail-gr-alert gmail-gr_spell gmail-gr_inline_cards gmail-gr_run_anim gmail-ContextualSpelling gmail-ins-del gmail-multiReplace" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">`evaluate <span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30)">myButLast [</span><span style="text-decoration-style:initial;text-decoration-color:initial;font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30);color:rgb(181,206,168)">1</span><span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30)">]</span></span></span>`</div><div><span style="font-size:small;text-align:left;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><span class="gmail-gr_ gmail-gr_197 gmail-gr-alert gmail-gr_spell gmail-gr_inline_cards gmail-gr_run_anim gmail-ContextualSpelling gmail-ins-del gmail-multiReplace" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit"><span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30)"><br></span></span></span></div><div>One more note: maybe you know we usually should not use `error`, which raises an error in a pure function.<br>So, we usually handle errors in an `IO` context. That's why `shouldThrow` receives an `IO a`.</div><div><span style="font-size:small;text-align:left;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><span class="gmail-gr_ gmail-gr_197 gmail-gr-alert gmail-gr_spell gmail-gr_inline_cards gmail-gr_run_anim gmail-ContextualSpelling gmail-ins-del gmail-multiReplace" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit"><span style="text-decoration-style:initial;text-decoration-color:initial;color:rgb(212,212,212);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;text-align:start;white-space:pre-wrap;background-color:rgb(30,30,30)"><br></span></span></span></div><div><span style="font-size:small;text-align:left;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline"><span class="gmail-gr_ gmail-gr_197 gmail-gr-alert gmail-gr_spell gmail-gr_inline_cards gmail-gr_run_anim gmail-ContextualSpelling gmail-ins-del gmail-multiReplace" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit"><br></span></span></div></div><br><div class="gmail_quote"><div dir="ltr">2018年7月9日(月) 2:57 Paweł Bałaga <<a href="mailto:ppbalaga@gmail.com">ppbalaga@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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_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="m_3668971401139966390gmail-adL"><br></div></div></div></div>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>山本悠滋<br>twitter: @igrep<br>GitHub: <a href="https://github.com/igrep" target="_blank">https://github.com/igrep</a></div><div>GitLab: <a href="https://gitlab.com/igrep" target="_blank">https://gitlab.com/igrep</a><br>Facebook: <a href="http://www.facebook.com/igrep" target="_blank">http://www.facebook.com/igrep</a><br>Google+: <a href="https://plus.google.com/u/0/+YujiYamamoto_igrep" target="_blank">https://plus.google.com/u/0/+YujiYamamoto_igrep</a></div></div></div>