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: EitherT (mike h) 2. Lazyness and forM_ (Marc Busqué) 3. Re: Lazyness and forM_ (Kim-Ee Yeoh) ---------------------------------------------------------------------- Message: 1 Date: Fri, 13 Apr 2018 09:35:01 +0100 From: mike h <mike_k_hough...@yahoo.co.uk> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] EitherT Message-ID: <0709c844-fd6e-4130-b959-59ea459f7...@yahoo.co.uk> Content-Type: text/plain; charset="utf-8" Even ‘better’ instance Monad m => Functor (EitherT m a) where fmap f m = EitherT $ runEitherT m >>= \mv -> return $ fmap f mv :) > On 12 Apr 2018, at 21:32, mike h <mike_k_hough...@yahoo.co.uk> wrote: > > instance Monad m => Functor (EitherT m a) where > fmap f m = EitherT $ do > mv <- runEitherT m > case mv of > Left lv -> return $ Left lv > Right rv -> return $ Right (f rv) > > > Thanks all :) I think its correct. The compiler does! > Mike > > >> On 12 Apr 2018, at 17:27, David McBride <toa...@gmail.com >> <mailto:toa...@gmail.com>> wrote: >> >> You will feel like this was obvious in hindsight. >> >> fmap :: (a -> b) -> f a -> f b >> fmap (a -> b) -> EitherT m c a -> EitherT m c b >> >> Now follow the types in your code. >> m :: EitherT m c a >> mv :: Either c a >> return mv :: EitherT m c a -- <- you are back to the wrong type. >> >> How can you instead return EitherT m c b? >> >> >> On Thu, Apr 12, 2018 at 6:25 AM, mike h <mike_k_hough...@yahoo.co.uk >> <mailto:mike_k_hough...@yahoo.co.uk>> wrote: >> Hi, >> I’m trying to write EitherT from first principles and I’m stuck at the first >> hurdle - Functor. I’m looking for a hint rather than a complete answer :) >> >> >> This is what I have >> >> newtype EitherT m a b = EitherT {runEitherT :: m (Either a b)} >> instance Monad m => Functor (EitherT m a) where >> ---- fmap :: (a -> b) -> f a -> f b >> fmap f m = EitherT $ do >> mv <- runEitherT m >> case mv of >> Left _ -> return mv >> Right rv -> return $ Right (f rv) >> >> >> >> and here is the compilers view >> Phrase.hs:31:25: error: >> • Couldn't match type ‘b’ with ‘a1’ >> ‘b’ is a rigid type variable bound by >> the type signature for: >> fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b >> at Phrase.hs:27:5-8 >> ‘a1’ is a rigid type variable bound by >> the type signature for: >> fmap :: forall a1 b. (a1 -> b) -> EitherT m a a1 -> EitherT m a b >> at Phrase.hs:27:5-8 >> Expected type: m (Either a a1) >> Actual type: m (Either a b) >> • In the expression: return $ Right (f rv) >> In a case alternative: Right rv -> return $ Right (f rv) >> In a stmt of a 'do' block: >> case mv of >> Left _ -> return mv >> Right rv -> return $ Right (f rv) >> • Relevant bindings include >> rv :: a1 (bound at Phrase.hs:31:19) >> mv :: Either a a1 (bound at Phrase.hs:28:9) >> m :: EitherT m a a1 (bound at Phrase.hs:27:12) >> f :: a1 -> b (bound at Phrase.hs:27:10) >> fmap :: (a1 -> b) -> EitherT m a a1 -> EitherT m a b >> (bound at Phrase.hs:27:5) >> >> >> what I think I need to do is fmap over the right value after pulling if out >> of the monad m by doing mv <- runEitherT m >> >> these lines from the compiler are particularly confusing >> Expected type: m (Either a a1) >> Actual type: m (Either a b) >> >> as I believe f is f:: a1->b >> >> So just hints please and I expect I’ll have another duh moment. >> >> Thanks >> >> Mike >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org <mailto:Beginners@haskell.org> >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org <mailto: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/20180413/b774bbe0/attachment-0001.html> ------------------------------ Message: 2 Date: Fri, 13 Apr 2018 11:42:20 +0200 (CEST) From: Marc Busqué <m...@lamarciana.com> To: beginners@haskell.org Subject: [Haskell-beginners] Lazyness and forM_ Message-ID: <alpine.LNX.2.21.1804131127290.6944@localhost> Content-Type: text/plain; charset="iso-8859-15"; Format="flowed" Hi there! I'm using [selda](https://github.com/valderman/selda) package to work with databases. I'm trying to write a function to generate several tables at once. In the following examples, `categories` and `expenses` are two selda `Table`. The other functions in use and their respective imports should be self-evident: If I do: ``` migrate :: IO () migrate = do dir <- dBDir createDirectoryIfMissing True dir forM_ (categories, expenses) $ withDB . createTable ``` tables are not actually created. However, if I do: ``` migrate :: IO () migrate = do dir <- dBDir createDirectoryIfMissing True dir withDB . createTable $ categories withDB . createTable $ expenses ``` Both tables are actually created. So it seems like the thugs created with `formM_` are actually never executed. Is it so? I'm new with Haskell and it seems strange for me. If it is so, doesn't make it useless `forM_` for IO? Furthermore, I can't reproduce it in ghci. Doing this: ``` ["a", "b"] `forM_` print ``` Actually prints both `"a"` and `"b"`. Thanks for any enlightment. Marc Busqué http://waiting-for-dev.github.io/about/ ------------------------------ Message: 3 Date: Fri, 13 Apr 2018 17:53:28 +0700 From: Kim-Ee Yeoh <k...@atamo.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Lazyness and forM_ Message-ID: <CAPY+ZdRr72uyTju7wduJd=hatrswfprsumgkc1nz-ueaftb...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hi Marc, and welcome to the haskell beginners list. ``` > migrate :: IO () > migrate = do > dir <- dBDir > createDirectoryIfMissing True dir > forM_ (categories, expenses) > $ withDB . createTable > ``` > > tables are not actually created. > > Well, forM_ (categories, expenses) $ withDB . createTable is equivalent to withDB . createTable $ expenses. So exactly one table is created. > ``` > ["a", "b"] `forM_` print > ``` > > Actually prints both `"a"` and `"b"`. > > Here the code uses square brackets--and so we have honest-to-goodness lists--whereas the previous used parentheses. See what happens with: ("a","b") `forM_` print. Marc: Feel free to write to the haskell-cafe mailing list for questions such as this. Fortuitously in this case, it turns out that your query needed knowledge only about the forM* combinators and--ever since their ilk was generalized--the known instances declared for the Traversable constraint. As you explore the domain-specific package "selda" further, you will find more people acquainted with the package over at the cafe than here in beginners. Suffice to say, everyone here in this list is also in cafe, which is also open to beginners questions. p.s. Veterans would recognize this as ye olde controversy on the Foldable instance for pairs introduced in GHC 7.10.x. The controversy still simmers apparently because there isn't an instance for triples and higher tuples in the latest and greatest GHC 8.4.1. We are at the mercy of this potentially toe-stubbing absence of uniformity. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180413/8addc080/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 118, Issue 8 *****************************************