<div dir="ltr">Here's what I finally did<div><br></div><div><font face="monospace">myIndex'' l n<br> | m == Nothing = error "No list."<br> | otherwise = fromJust m<br> where m = mI l n<br> mI [] _ = Nothing<br> mI (h:t) n | n == 0 = Just h<br> | otherwise = mI t (n-1)</font></div><div><font face="monospace"><br></font></div><div><font face="arial, sans-serif">but then I can't say why I went to this extra step.</font><br><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Mar 6, 2021 at 10:53 AM Francesco Ariis <<a href="mailto:fa-ml@ariis.it" target="_blank">fa-ml@ariis.it</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">Il 06 marzo 2021 alle 09:53 Galaxy Being ha scritto:<br>
> I've got this example from the Internet<br>
> <br>
> import Data.List<br>
> import Data.Maybe<br>
> <br>
> firstFactorOf x<br>
> | m == Nothing = x<br>
> | otherwise = fromJust m<br>
> where m =(find p [2..x-1])<br>
> p y = mod x y == 0<br>
> <br>
> -- myIndex :: [a] -> Int -> Maybe a<br>
> myIndex [] _ = Nothing<br>
> myIndex (x:xs) 0 = Just x<br>
> myIndex (x:xs) n = myIndex xs (n-1)<br>
> <br>
> I would like the Just x in the second block to actually be fromJust x as in<br>
> the first block, i.e., I want a number returned, not a Just typed object.<br>
> I've tried changing Just x to fromJust x but get the error when I try to<br>
> use it<br>
<br>
What would happen in the `Nothing` case? If an error is fine with you:<br>
<br>
myUnsafeIndex :: [a] -> Int -> a<br>
myUnsafeIndex as n =<br>
case myIndex as n of<br>
Nothing -> error "Chiare, fresche et dolci acque"<br>
-- or maybe return -1? Idk<br>
Just r -> r<br>
<br>
What have you written instead<br>
<br>
> Also, my type declaration seems to be wrong too, but I don't see why.<br>
<br>
It compiles fine here, even if I remove the comment from `myIndex`<br>
signature<br>
—F<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>