[Haskell-beginners] Maybe problems converting back to number
Galaxy Being
borgauf at gmail.com
Sat Mar 6 15:53:30 UTC 2021
I've got this example from the Internet
import Data.List
import Data.Maybe
firstFactorOf x
| m == Nothing = x
| otherwise = fromJust m
where m =(find p [2..x-1])
p y = mod x y == 0
and this as a crude return the nth element of a list
import Data.List
import Data.Maybe
-- myIndex :: [a] -> Int -> Maybe a
myIndex [] _ = Nothing
myIndex (x:xs) 0 = Just x
myIndex (x:xs) n = myIndex xs (n-1)
I would like the Just x in the second block to actually be fromJust x as in
the first block, i.e., I want a number returned, not a Just typed object.
I've tried changing Just x to fromJust x but get the error when I try to
use it
> myIndex [1,2,3,4,5] 3
* Non type-variable argument
: in the constraint: Num (Maybe (Maybe a))
: (Use FlexibleContexts to permit this)
: * When checking the inferred type
: it :: forall a. Num (Maybe (Maybe a)) => Maybe a
What am I missing here? Also, my type declaration seems to be wrong too,
but I don't see why.
LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210306/95b6a5f4/attachment.html>
More information about the Beginners
mailing list