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: Adding Either around a List monad? (Benjamin Edwards) 2. Re: Adding Either around a List monad? (Benjamin Edwards) 3. Re: File download using wreq. (Mike Houghton) ---------------------------------------------------------------------- Message: 1 Date: Mon, 05 Oct 2015 13:21:30 +0000 From: Benjamin Edwards <edwards.b...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Adding Either around a List monad? Message-ID: <CAN6k4njrWqcEYq2+bu-HUeYEpDDn6MQ+YBPM3_T=mvfwthv...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" If you want to use monad transformers and have Either e [a] as the result type then you need Either to be the inner monad and List to be the outer monad. If you look at the types of EitherT (from the either package) and ListT from transformers this should hopefully make sense. Then you would keep the same impl as you have now, only you would need to "run" the ListT computation to yield Either e [a]. Anything that you would like to do inside of the inner Error monad will need to lifted inside of it using lift. Does that help you at all? Ben On Mon, 5 Oct 2015 at 11:06 Mario Lang <ml...@delysid.org> wrote: > Hi. > > Consider this structure: > > vs :: Rational -> [Input] -> [[Output]] > vs _ [] = return [] > vs l (x:xs) = pms l x >>= \pm -> (pm :) <$> vs (l - dur pm) xs > > pms :: Rational -> Input -> [Output] > pms l x = [x, x+1, x+2, ...] -- Just an example, not real code. > -- in reality, l is used to determine > -- the result of pms. > > This is basically traverse, but with a state (l) added to it. > So without the state, vs could be written as > > vs = traverse pms > > Now, I want to add Either e to this, like: > > vs :: Rational -> [Input] -> Either e [[Output]] > pms :: Rational -> Input -> Either e [Output] > > However, I have no idea how to implement vs. > > Interestingly, adding Either e to vs without changing the code lets it > compile, but it gives me the wrong result: > > vs :: Rational -> [Input] -> Either e [[Output]] > vs _ [] = return [] > vs l (x:xs) = pms l x >>= \pm -> (pm :) <$> vs (l - pm) xs > > Since I am in the Either monad now, >>= does not do non-determinism, it > simply unwraps the Either from pms. I have to admit, I dont fully > understand why this compiles, and what exactly it does wrong. I only > see from testing that the results can't be right. > > On IRC, Gurkenglas suggested to use the State monad, like this: > > vs :: Rational -> [Input] -> Either e [[Output]] > vs l = `evalStateT l` . mapM v where > v x = do l <- get > pm <- lift $ pms l x > put (l - dur pm) > return pm > > This compiles, but also yields unexpected results. > > I have invested several hours now trying to add Either around this > algorithm, so that I can emit hard failures. I am sort of frustrated > and out of ideas. Somehow, I can't figure out what these > transformations actually change in behaviour. I am being told, by quite > experienced Haskell programmers, that this is supposed to be correct, > but my testing tells me otherwise. So before I just give up on this, > could someone please have a look and let me know if I have missed > something obvious? > > -- > CYa, > ????? > _______________________________________________ > 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/20151005/d42e392a/attachment-0001.html> ------------------------------ Message: 2 Date: Mon, 05 Oct 2015 13:35:32 +0000 From: Benjamin Edwards <edwards.b...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Adding Either around a List monad? Message-ID: <CAN6k4ngmmfeLZEfuuvPP3H=Ryi0fc6VV0Ue+3Cc-gdRuK3+a=q...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Or in fact, are you just asking about a much simpler problem: traverse :: (a -> f b) -> t a -> f (t b) So instantiating f a to Either String [Int] (but Int could be anything) and t a to [Int] then: f :: Int -> Either String [Int] f = Right . pure . (*6) This would yield what you would expect. If you had a more complicated function that potentially used Left "ERROR", you could short circuit the computation. Ben On Mon, 5 Oct 2015 at 14:21 Benjamin Edwards <edwards.b...@gmail.com> wrote: > If you want to use monad transformers and have Either e [a] as the result > type then you need Either to be the inner monad and List to be the outer > monad. If you look at the types of EitherT (from the either package) and > ListT from transformers this should hopefully make sense. Then you would > keep the same impl as you have now, only you would need to "run" the ListT > computation to yield Either e [a]. Anything that you would like to do > inside of the inner Error monad will need to lifted inside of it using > lift. Does that help you at all? > > Ben > > On Mon, 5 Oct 2015 at 11:06 Mario Lang <ml...@delysid.org> wrote: > >> Hi. >> >> Consider this structure: >> >> vs :: Rational -> [Input] -> [[Output]] >> vs _ [] = return [] >> vs l (x:xs) = pms l x >>= \pm -> (pm :) <$> vs (l - dur pm) xs >> >> pms :: Rational -> Input -> [Output] >> pms l x = [x, x+1, x+2, ...] -- Just an example, not real code. >> -- in reality, l is used to determine >> -- the result of pms. >> >> This is basically traverse, but with a state (l) added to it. >> So without the state, vs could be written as >> >> vs = traverse pms >> >> Now, I want to add Either e to this, like: >> >> vs :: Rational -> [Input] -> Either e [[Output]] >> pms :: Rational -> Input -> Either e [Output] >> >> However, I have no idea how to implement vs. >> >> Interestingly, adding Either e to vs without changing the code lets it >> compile, but it gives me the wrong result: >> >> vs :: Rational -> [Input] -> Either e [[Output]] >> vs _ [] = return [] >> vs l (x:xs) = pms l x >>= \pm -> (pm :) <$> vs (l - pm) xs >> >> Since I am in the Either monad now, >>= does not do non-determinism, it >> simply unwraps the Either from pms. I have to admit, I dont fully >> understand why this compiles, and what exactly it does wrong. I only >> see from testing that the results can't be right. >> >> On IRC, Gurkenglas suggested to use the State monad, like this: >> >> vs :: Rational -> [Input] -> Either e [[Output]] >> vs l = `evalStateT l` . mapM v where >> v x = do l <- get >> pm <- lift $ pms l x >> put (l - dur pm) >> return pm >> >> This compiles, but also yields unexpected results. >> >> I have invested several hours now trying to add Either around this >> algorithm, so that I can emit hard failures. I am sort of frustrated >> and out of ideas. Somehow, I can't figure out what these >> transformations actually change in behaviour. I am being told, by quite >> experienced Haskell programmers, that this is supposed to be correct, >> but my testing tells me otherwise. So before I just give up on this, >> could someone please have a look and let me know if I have missed >> something obvious? >> >> -- >> CYa, >> ????? >> _______________________________________________ >> 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/20151005/aa4e0587/attachment-0001.html> ------------------------------ Message: 3 Date: Mon, 5 Oct 2015 16:03:39 +0100 From: Mike Houghton <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] File download using wreq. Message-ID: <9d9b9020-9e93-4a60-ab9f-4664c67f1...@yahoo.co.uk> Content-Type: text/plain; charset="utf-8" Thank you. I?ll work through what you?ve written. > On 5 Oct 2015, at 09:21, Kostiantyn Rybnikov <k...@k-bx.com> wrote: > > Just few more things which might help: > > ? res ^. responseStatus > Status {statusCode = 200, statusMessage = "OK"} > ? :t res ^. responseStatus > res ^. responseStatus :: Status > ? :i Status > data Status > = Network.HTTP.Types.Status.Status {Network.HTTP.Types.Status.statusCode :: > Int, > Network.HTTP.Types.Status.statusMessage > :: Data.ByteString.Internal.ByteString} > -- Defined in ?Network.HTTP.Types.Status? > instance Enum Status -- Defined in ?Network.HTTP.Types.Status? > instance Eq Status -- Defined in ?Network.HTTP.Types.Status? > instance Ord Status -- Defined in ?Network.HTTP.Types.Status? > instance Show Status -- Defined in ?Network.HTTP.Types.Status? > > You can see how to get the status, where it comes from. So you can just do > "if res ^. responseStatus /= status200 then ...". > > Cheers. > > On Sun, Oct 4, 2015 at 6:18 PM, Mike Houghton <mike_k_hough...@yahoo.co.uk > <mailto:mike_k_hough...@yahoo.co.uk>> wrote: > Hi, > > Please can someone explain how,using the wreq package, I can download and > save a binary file? > Say the file is at the end of > http://x.y.z/files/myfile.jpg <http://x.y.z/files/myfile.jpg> > > and it is a jpeg and no authentication is needed. > > I just want to > > 1. Check that the URL is syntactically correct - flag and error if not > 2. If the URL is syntactically ok then download the file using GET. > 3. Check that the response code is 200 and if so save the file > 3a. if the response code is not 200 then delegate to an error handling > function or some simple idiomatic way of error handling. > > > Thanks once again. > > 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 > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151005/a524c03f/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 88, Issue 4 ****************************************