<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Op 31-10-2015 om 17:43 schreef Roelof
Wobben:<br>
</div>
<blockquote cite="mid:5634EFB9.8000606@home.nl" type="cite">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<div class="moz-cite-prefix">Op 31-10-2015 om 14:56 schreef Roel
van Dijk:<br>
</div>
<blockquote
cite="mid:CABw4ky58Q4C-u0p+ALF+LoX1uirjiujABOaWEEJEObvZCj=M-w@mail.gmail.com"
type="cite">
<div dir="ltr">You are even closer now!
<div><br>
</div>
<div>What the type checker is trying to tell you is that it
doesn't know how to raise <font face="monospace, monospace">Maybe
Integer</font> to some power.</div>
<div><br>
</div>
<div>You apply the ^ operator to two values, <span
style="font-size:12.8px"><font face="monospace, monospace">f2Maybe
(n `div` 2)</font> and <font face="monospace,
monospace">2</font>. Let us give them names:</span></div>
<div><span style="font-size:12.8px"><br>
</span></div>
<div><font face="monospace, monospace"><span
style="font-size:12.8px">let a = </span><span
style="font-size:12.8px">f2Maybe (n `div` 2) :: Maybe
Integer</span></font></div>
<div><span style="font-size:12.8px"><font face="monospace,
monospace"> b = 2 :: Int</font></span></div>
<div><span style="font-size:12.8px"><font face="monospace,
monospace">in a ^ b</font></span></div>
<div><span style="font-size:12.8px"><br>
</span></div>
<div><span style="font-size:12.8px">You see that the type of <font
face="monospace, monospace">a</font><font face="arial,
helvetica, sans-serif"> is </font><font
face="monospace, monospace">Maybe Integer</font><font
face="arial, helvetica, sans-serif">. What does this
mean? There are only 2 cases to consider. You have </font><font
face="monospace, monospace">Just </font><font
face="arial, helvetica, sans-serif">an integer or you
have </font><font face="monospace, monospace">Nothing</font><font
face="arial, helvetica, sans-serif">.</font></span></div>
<div><span style="font-size:12.8px"><font face="arial,
helvetica, sans-serif"><br>
</font></span></div>
<div><span style="font-size:12.8px"><font face="arial,
helvetica, sans-serif">You can use the </font><font
face="monospace, monospace">case </font><font
face="arial, helvetica, sans-serif">construct to write
the code for both cases.</font></span></div>
<div><span style="font-size:12.8px"><font face="arial,
helvetica, sans-serif"><br>
</font></span></div>
<div><font face="monospace, monospace"><span class="im"
style="font-size:12.8px">f2Maybe :: Integer -> Maybe
Integer<br>
f2Maybe n<br>
| n > 0 = Nothing<br>
| n == 0 = Just 1<br>
</span><span style="font-size:12.8px"> | even n = case
f2Maybe (n `div` 2) of</span></font></div>
<div><font face="monospace, monospace"><span
style="font-size:12.8px"> Just x ->
<fill in></span></font></div>
<div><font face="monospace, monospace"> Nothing
-> <fill in><br style="font-size:12.8px">
<span style="font-size:12.8px"> | odd n = case f2Maybe
(n `div` 2) of</span></font></div>
<div><font face="monospace, monospace"><span
style="font-size:12.8px"> Just x ->
<fill in></span></font><span
style="font-size:12.8px"><font face="arial, helvetica,
sans-serif"><br>
</font></span></div>
<div><font face="monospace, monospace"><span
style="font-size:12.8px"> Nothing ->
<fill in></span></font></div>
<div><br>
</div>
</div>
</blockquote>
<br>
<br>
Still confusing. <br>
<br>
with even you can have a integer which will be a integer or
nothing if there is a number which is not even. <br>
So with 1 it will be Nothing. <br>
<br>
but will the odd even be reached ? <br>
<br>
<br>
Roelof<br>
<br>
</blockquote>
<br>
<br>
Solved both ones like this : <br>
<br>
f2Maybe :: Integer -> Maybe Integer <br>
f2Maybe n <br>
| n < 0 = Nothing <br>
| otherwise = Just (f3 n )<br>
<br>
f3 :: Integer -> Integer <br>
f3 n <br>
| n == 0 = 1 <br>
| even n = f3 ( n `div` 2) ^ 2<br>
| odd n = (f3 ( n `div` 2) ^ 2) * 2 <br>
<br>
<br>
f2Either :: Integer -> Either String Integer <br>
f2Either n <br>
| n < 0 = Left "This function works only with positive
number"<br>
| otherwise = Right (f3 n) <br>
<br>
So I can now test on Nothing or Left "This function works only with
positive number" ? <br>
<br>
Roelof<br>
<br>
<br>
<br>
<br>
</body>
</html>