Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/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: Type signature question (Gesh hseG) 2. Re: Type signature question (Louis-Guillaume Gagnon) 3. Re: Type signature question (Chadda? Fouch?) 4. Re: Why does this list comprehension return an empty list? (Chadda? Fouch?) 5. Pipe and typeclasses (Emmanuel Surleau) 6. Re: Pipe and typeclasses (Karol Samborski) 7. Re: Shorten this code (Kim-Ee Yeoh) 8. Re: Why does this list comprehension return an empty list? (Kim-Ee Yeoh) ---------------------------------------------------------------------- Message: 1 Date: Tue, 23 Jul 2013 18:05:51 +0300 From: Gesh hseG <g...@gesh.uni.cx> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Type signature question Message-ID: <CACS5XqMp12OvW3vHrVb=XFfH=gt95ytg9s2ngzsrqotji0d...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Tue, Jul 23, 2013 at 5:48 PM, Denis Kasak <denis.ka...@gmail.com> wrote: > On 23 July 2013 16:33, Louis-Guillaume Gagnon > <louis.guillaume.gag...@gmail.com> wrote: >> >> and I don't undestand why the "Eq a =>" shows up. > The inference works somewhat like this: GHC sees the definitions of firstHalf, secondHalf and infers they have the same type as xs, and that all three of them must be some lists for them to be passed to and be returned by take and drop. GHC sees firstHalf == secondHalf. Since (==) has type Eq a => a -> a -> Bool, firstHalf (and secondHalf and xs) must have types which are instances of the typeclass Eq. Looking at the instance declarations for Eq, we find that for all types t which are instances of Eq, the type [t] (lists of values of type t) is also an instance of Eq. In other words, instance Eq a => Eq [a] where ... Thus, we have that firstHalf, secondHalf, xs :: Eq t => [t] and cannot infer a more specific type for these three names, leaving us with the signature Eq t => [t] -> Bool for isPalindrome. Intuitively, since you're checking whether the first half of the list is equal to the second half, you need to be able to check that the first element is equal to the last, second to the before-last, etc. Also, notice that you could just have written isPalindrome xs = xs == reverse xs or even isPalindrome = (==) <$> id <*> reverse but this last way is too obfuscated for most sane people. HTH, Gesh ------------------------------ Message: 2 Date: Tue, 23 Jul 2013 11:21:16 -0400 From: Louis-Guillaume Gagnon <louis.guillaume.gag...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Type signature question Message-ID: <CANCBZJvockekLGVU6RqwikxuWe=oas1f1+sgmd3q0gazcqp...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 2013/7/23 Gesh hseG <g...@gesh.uni.cx>: > The inference works somewhat like this: > GHC sees the definitions of firstHalf, secondHalf and infers they have > the same type as xs, > and that all three of them must be some lists for them to be passed to > and be returned by take and drop. > GHC sees firstHalf == secondHalf. Since (==) has type Eq a => a -> a > -> Bool, firstHalf (and secondHalf and xs) > must have types which are instances of the typeclass Eq. > Looking at the instance declarations for Eq, we find that for all > types t which are instances of Eq, the type [t] > (lists of values of type t) is also an instance of Eq. > In other words, instance Eq a => Eq [a] where ... > > Thus, we have that firstHalf, secondHalf, xs :: Eq t => [t] and cannot > infer a more specific type for these three names, > leaving us with the signature Eq t => [t] -> Bool for isPalindrome. > > Intuitively, since you're checking whether the first half of the list > is equal to the second half, you need to be able to > check that the first element is equal to the last, second to the > before-last, etc. great post, thanks! > Also, notice that you could just have written > isPalindrome xs = xs == reverse xs Why didn't I think of that!? glg ------------------------------ Message: 3 Date: Tue, 23 Jul 2013 19:48:14 +0200 From: Chadda? Fouch? <chaddai.fou...@gmail.com> To: beginners <beginners@haskell.org> Subject: Re: [Haskell-beginners] Type signature question Message-ID: <canfjzrbsu25xufjkxxbutywaywakscrcvdfaoiwlfsbz+4u...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Le 23 juil. 2013 17:23, "Louis-Guillaume Gagnon" < louis.guillaume.gag...@gmail.com> a ?crit : > > > Also, notice that you could just have written > > isPalindrome xs = xs == reverse xs > > Why didn't I think of that Probably because your internal definition of palindromes is slightly wrong : note that your function doesn't do the same thing as this one, more precisely it refuse any palindrome of odd length like [1, 2, 3, 2, 1]. Of course you may have your own definition that makes even length a requirement. -- Jeda? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130723/8e40a548/attachment-0001.html> ------------------------------ Message: 4 Date: Tue, 23 Jul 2013 21:12:51 +0200 From: Chadda? Fouch? <chaddai.fou...@gmail.com> To: beginners <beginners@haskell.org> Subject: Re: [Haskell-beginners] Why does this list comprehension return an empty list? Message-ID: <CANfjZRY30CTA+L6fV4W=6jpoxxlejin_apxpmyv0wr_tzas...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Le 23 juil. 2013 10:54, "Costello, Roger L." <coste...@mitre.org> a ?crit : > > Hi Folks, > > I have a list of singletons: > > xs = [("a")] > > f is a function that, given an argument x, it returns the argument: > > f x = x > > g is a function that, given an argument x, it returns the empty list: > > g x = [] > > I have a list comprehension that extracts the singletons from xs using f and g, and creates a pair from their output: > > [(a,b) | a <- f xs, b <- g xs] > > I executed this and the result is the empty list: > > [] > > That is odd. Why is the empty list the result? This is pretty normal since there are no elements in "g xs" so b can takes no values. You have got some excellent answers on that but I think your problem is more fundamental : > f x = x This function does nothing or more precisely it is the identity, you can replace "f anything" by "anything" and have exactly the same result. > g x = [] This function just takes anything and returns an empty list. So > [(a,b) | a <- f xs, b <- g xs] Is exactly the same as : > [(a,b) | a <- xs, b <- [] ] If xs is ["a"] that becomes > [(a,b) | a <- ["a"], b <- [] ] I think those f and g didn't work like that in your mind so could you explain what you thought they should do (with some examples maybe). -- Jeda? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130723/0515b03e/attachment-0001.html> ------------------------------ Message: 5 Date: Tue, 23 Jul 2013 21:48:06 +0200 From: Emmanuel Surleau <emmanuel.surl...@gmail.com> To: Haskell Beginners <beginners@haskell.org> Subject: [Haskell-beginners] Pipe and typeclasses Message-ID: <20130723194806.GA9606@emm-laptop> Content-Type: text/plain; charset=us-ascii Hello, I have seen from time to time the pipe symbol in type signatures, like so: class Monad m => MonadReader r m | m -> r where How should I interpret that ? Or more to the point, how does the compiler interpret it? Thanks, Emm ------------------------------ Message: 6 Date: Tue, 23 Jul 2013 21:56:20 +0200 From: Karol Samborski <edv.ka...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Pipe and typeclasses Message-ID: <cace2dtt3hh8sdmw+6+kixrnfxlyp6xyd2hdujihe7dgkndv...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 2013/7/23 Emmanuel Surleau <emmanuel.surl...@gmail.com> > Hello, > > I have seen from time to time the pipe symbol in type signatures, like so: > > class Monad m => MonadReader r m | m -> r where > > How should I interpret that ? Or more to the point, how does the compiler > interpret it? > Hi Emmanuel! The pipe symbol is for a functional dependency: http://www.haskell.org/haskellwiki/Functional_dependencies Karol -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130723/906f306c/attachment-0001.html> ------------------------------ Message: 7 Date: Wed, 24 Jul 2013 07:36: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] Shorten this code Message-ID: <capy+zdrhv8pofdtuu32_derxetyzgqnnxo_rqwh-fufcmkd...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" On Tue, Jul 23, 2013 at 5:41 PM, Nadav Chernin <nadavcher...@gmail.com>wrote: > Please try to shorten it: > What would you achieve by doing so? -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130724/9097a38b/attachment-0001.html> ------------------------------ Message: 8 Date: Wed, 24 Jul 2013 07:47:24 +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] Why does this list comprehension return an empty list? Message-ID: <capy+zdrowvpcuvk5sva11fq9f9+2pntetqgrgbulqfmcnto...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" On Tue, Jul 23, 2013 at 3:53 PM, Costello, Roger L. <coste...@mitre.org>wrote: > Also, how would I modify this: > > [(a,b) | a <- f xs, b <- g xs] > > so that it produces this: > > [("a",[])] > Try: [(a,b) | a <- f xs | b <- g xs] Note the 2nd parallel bar. You may have to enable a Parallel List Comprehension pragma or somesuch. Everyone else has given great explanations about your curious definitions of f and g and why the original does what it does. Well worth looking into. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130724/34c13d88/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 61, Issue 27 *****************************************