Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Maybe problems converting back to number (Galaxy Being) 2. Re: Maybe problems converting back to number (Francesco Ariis) 3. Re: Maybe problems converting back to number (Galaxy Being) ---------------------------------------------------------------------- Message: 1 Date: Sat, 6 Mar 2021 09:53:30 -0600 From: Galaxy Being <borg...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Maybe problems converting back to number Message-ID: <CAFAhFSVen27BZfwD2Vo=4Mi4cC=ouof9pmi_uwyu1b1unwv...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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-0001.html> ------------------------------ Message: 2 Date: Sat, 6 Mar 2021 17:52:16 +0100 From: Francesco Ariis <fa...@ariis.it> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Maybe problems converting back to number Message-ID: <20210306165216.GB2849@extensa> Content-Type: text/plain; charset=utf-8 Il 06 marzo 2021 alle 09:53 Galaxy Being ha scritto: > 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 > > -- 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 What would happen in the `Nothing` case? If an error is fine with you: myUnsafeIndex :: [a] -> Int -> a myUnsafeIndex as n = case myIndex as n of Nothing -> error "Chiare, fresche et dolci acque" -- or maybe return -1? Idk Just r -> r What have you written instead > Also, my type declaration seems to be wrong too, but I don't see why. It compiles fine here, even if I remove the comment from `myIndex` signature —F ------------------------------ Message: 3 Date: Sat, 6 Mar 2021 22:17:26 -0600 From: Galaxy Being <borg...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Maybe problems converting back to number Message-ID: <CAFAhFSVPkBfVOBxR-KzwO3GGoNKV+9-AV691AGSSbhmk=x-...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Here's what I finally did myIndex'' l n | m == Nothing = error "No list." | otherwise = fromJust m where m = mI l n mI [] _ = Nothing mI (h:t) n | n == 0 = Just h | otherwise = mI t (n-1) but then I can't say why I went to this extra step. On Sat, Mar 6, 2021 at 10:53 AM Francesco Ariis <fa...@ariis.it> wrote: > Il 06 marzo 2021 alle 09:53 Galaxy Being ha scritto: > > 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 > > > > -- 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 > > What would happen in the `Nothing` case? If an error is fine with you: > > myUnsafeIndex :: [a] -> Int -> a > myUnsafeIndex as n = > case myIndex as n of > Nothing -> error "Chiare, fresche et dolci acque" > -- or maybe return -1? Idk > Just r -> r > > What have you written instead > > > Also, my type declaration seems to be wrong too, but I don't see why. > > It compiles fine here, even if I remove the comment from `myIndex` > signature > —F > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210306/c946bd5c/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 152, Issue 3 *****************************************