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. Re: how to skip pattern match error when applying a mapM_ (Manuel Vázquez Acosta) 2. Re: how to skip pattern match error when applying a mapM_ (PICCA Frederic-Emmanuel) 3. Re: how to skip pattern match error when applying a mapM_ (Daniel Trstenjak) ---------------------------------------------------------------------- Message: 1 Date: Tue, 17 Jan 2017 18:31:55 -0500 From: Manuel Vázquez Acosta <mva....@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] how to skip pattern match error when applying a mapM_ Message-ID: <87eg016z6c....@pavla.com> Content-Type: text/plain >From [1]: -- | Fail with a message. This operation is not part of the -- mathematical definition of a monad, but is invoked on pattern-match -- failure in a @do@ expression. -- -- As part of the MonadFail proposal (MFP), this function is moved -- to its own class 'MonadFail' (see "Control.Monad.Fail" for more -- details). The definition here will be removed in a future -- release. fail :: String -> m a fail s = errorWithoutStackTrace s Best regards, Manuel (also a newbie) [1] http://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Base.html#Monad > thanks and what is the purpose of > > fail _ = nothing > > Cheers > > Fred > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Message: 2 Date: Wed, 18 Jan 2017 10:28:30 +0000 From: PICCA Frederic-Emmanuel <frederic-emmanuel.pi...@synchrotron-soleil.fr> To: "The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell" <beginners@haskell.org> Subject: Re: [Haskell-beginners] how to skip pattern match error when applying a mapM_ Message-ID: <a2a20ec3b8560d408356cac2fc148e53bb329...@sun-dag3.synchrotron-soleil.fr> Content-Type: text/plain; charset="us-ascii" Hello, thanks for the informations after investigating, I could not switch the IO and the Maybe I need to process the IO in order to know if I have a Just or a Nothing So this is a IO (Maybe ...) I just would like to know if there is a better way to write this knowing len :: IO (Maybe Int) get_position' :: a -> b -> IO (Maybe Double) instance Frame DataFrameH5 where len d = lenH5Dataspace (h5delta d) row d idx = do n' <- len d case n' of (Just n) -> do let eof = n - 1 == idx let nxs' = h5nxs d let mu = 0.0 let komega = 0.0 let kappa = 0.0 let kphi = 0.0 gamma' <- get_position' (h5gamma d) 0 case gamma' of (Just gamma) -> do delta' <- get_position' (h5delta d) idx case delta' of (Just delta) -> do wavelength' <- get_position' (h5wavelength d) 0 case wavelength' of (Just wavelength) -> do let source = Source (head wavelength *~ nano meter) let positions = concat [mu, komega, kappa, kphi, gamma, delta] -- print positions let geometry = Geometry K6c source positions Nothing let detector = ZeroD m <- geometryDetectorRotationGet geometry detector poniext <- ponigen d (MyMatrix HklB m) idx return $ Just DifTomoFrame { difTomoFrameNxs = nxs' , difTomoFrameIdx = idx , difTomoFrameEOF = eof , difTomoFrameGeometry = geometry , difTomoFramePoniExt = poniext } Nothing -> return Nothing Nothing -> return Nothing Nothing -> return Nothing Nothing -> return Nothing where get_position' a b = do v <- get_position a b return $ if any isNaN v then Nothing else Just v Thanks for your help Frederic ------------------------------ Message: 3 Date: Wed, 18 Jan 2017 12:53:26 +0100 From: Daniel Trstenjak <daniel.trsten...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] how to skip pattern match error when applying a mapM_ Message-ID: <20170118115326.GA7930@octa> Content-Type: text/plain; charset=us-ascii Hi Frederic, > after investigating, I could not switch the IO and the Maybe > I need to process the IO in order to know if I have a Just or a Nothing > So this is a IO (Maybe ...) > > I just would like to know if there is a better way to write this > knowing There's the MaybeT[1] monad transformer for this use case. Instead of having: row :: t -> Int -> IO (Maybe (DifTomoFrame DIM1)) you would have: row :: t -> Int -> MaybeT IO (DifTomoFrame DIM1) So 'row' might look like: row d idx = do n <- len d let eof = n - 1 == idx let nxs' = h5nxs d let mu = 0.0 let komega = 0.0 let kappa = 0.0 let kphi = 0.0 gamma <- get_position' (h5gamma d) 0 delta <- get_position' (h5delta d) idx wavelength <- get_position' (h5wavelength d) 0 let source = Source (head wavelength *~ nano meter) let positions = concat [mu, komega, kappa, kphi, gamma, delta] -- print positions let geometry = Geometry K6c source positions Nothing let detector = ZeroD m <- lift $ geometryDetectorRotationGet geometry detector poniext <- lift $ ponigen d (MyMatrix HklB m) idx return $ DifTomoFrame { difTomoFrameNxs = nxs' , difTomoFrameIdx = idx , difTomoFrameEOF = eof , difTomoFrameGeometry = geometry , difTomoFramePoniExt = poniext } This assumes that the functions 'len', 'get_position' also return a 'MaybeT IO (...)'. Functions in the IO monad - like 'geometryDetectorRotationGet' and 'ponigen' - have to be "lifted" into the IO monad by 'lift'. To get at the 'Maybe' result of 'row' you're using the 'runMaybeT' function in the IO monad: main :: IO () main = do ... result <- runMaybeT (row t int) ... Greetings, Daniel [1] https://hackage.haskell.org/package/transformers-0.5.2.0/docs/Control-Monad-Trans-Maybe.html ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 103, Issue 16 ******************************************