<div dir="ltr"><div>I'm not getting past</div><div><br></div>whereIsBM boiList = case boiList of<br> Nothing -> Nothing<br> Just (Cons idx lx)<br> | (idx == Bacon) -> Just 1<br> | otherwise -> Just (1 + whereIsBM lx)<br><div><br></div><div>...and a few other attempts.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Mar 28, 2021 at 10:37 PM Jon Purdy <<a href="mailto:evincarofautumn@gmail.com">evincarofautumn@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div>‘whereIsBM’ returns a Maybe-wrapped value, so applying ‘1 + …’ to it would require ‘Maybe a’ to be in ‘Num’, hence the error message. ‘FlexibleContexts’ (ditto ‘FlexibleInstances’) is a pretty benign extension, but it won’t help here, since it just kicks the error down the road a bit.</div><div dir="auto"><br></div><div dir="auto">The basic thing you need to do is match on the Maybe and return ‘Nothing’ if it was ‘Nothing’, or ‘Just (1 + x)’ if it was ‘Just x’ for some x. That can be written quite literally as a ‘case’ expression:</div><div dir="auto"><br></div><div dir="auto">case whereIsBM lx of</div><div dir="auto"> Just x -> Just (1 + x)</div><div dir="auto"> Nothing -> Nothing</div><div dir="auto"><br></div><div dir="auto">Which could also be written with ‘do’:</div><div dir="auto"><br></div><div dir="auto">do</div><div dir="auto"> x <- whereIsBM lx</div><div dir="auto"> pure (1 + x)</div><div dir="auto"><br></div><div dir="auto">But this pattern is very common, so it’s already packaged up and generalised as ‘fmap’, a.k.a. ‘<$>’</div><div dir="auto"><br></div><div dir="auto">fmap (1 +) (whereIsBM lx)</div><div dir="auto">-- or</div><div dir="auto">(1 +) <$> whereIsBM lx<br><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Sun, Mar 28, 2021, 8:13 PM Galaxy Being <<a href="mailto:borgauf@gmail.com" target="_blank">borgauf@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I've got this <div><br></div><div><font face="monospace">import Data.Maybe<br><br>data MyList a = Empty | Cons a (MyList a) deriving (Eq,Ord,Show)<br>data BaconOrIndex = Bacon | Indx Int deriving (Eq,Ord,Show)<br><br>whereIsBM Empty = Nothing<br>whereIsBM (Cons idx lx) = if (idx == Bacon) then Just 1 else (whereIsBM lx)</font><br></div><div><br></div><div>which I would like to tell me where the <font face="monospace">Bacon</font> is (index), not just if there's <font face="monospace">Bacon,</font> which is what it does now. That is, I need this to happen</div><div><br></div><div>> <font face="monospace">whereIsBM (Cons (Indx 5) (Cons Bacon (Cons (Indx 2) (Cons (Indx 8) Empty))))<br></font></div><div><font face="monospace">Just 2</font></div><div><br></div><div>So I need to traverse a <font face="monospace">BaconOrIndex</font> list and count how deep I went to find the <font face="monospace">Bacon</font> variable. I get the above code to evaluate error-free, but obviously I'm only returning a <font face="monospace">Just 1 </font><font face="arial, sans-serif">when it sees </font><font face="monospace">Bacon</font><font face="arial, sans-serif">. What I need is to have the last part be</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif"> </font><font face="monospace">. . . else (1 + whereIsBM lx)</font></div><div><br></div><div> work; but it keeps giving the error<br></div><div><br></div><div><font face="monospace">Non type-variable argument in the constraint: Num (Maybe a)<br> (Use FlexibleContexts to permit this)<br> • When checking the inferred type<br> whereIsBM :: forall a.<br> (Num a, Num (Maybe a)) =><br> MyList BaconOrIndex -> Maybe a</font><br></div><div><br></div><div>I haven't a clue what this means. Eventually, I'll wrap this in something that handles the <font face="monospace">Nothing</font> and does <font face="monospace">fromJust </font>on the alternative. This whole effort is because if I didn't use the <font face="monospace">Maybe</font> strategy, and said </div><div><br></div><div><font face="monospace">whereIsBM Empty = 0</font></div><div><font face="monospace">...</font></div><div><br></div><div>it would never give back <font face="monospace">0</font> if it didn't find <font face="monospace">Bacon</font>, rather, it would simply return the whole countdown to <font face="monospace">Empty</font>. What can I do to make <font face="monospace">Maybe </font>work here?<br></div><div><br></div><div>LB</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 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></div></div>
</blockquote></div>