1 Apr 2005 10:09
3 Apr 2005 00:41
Ord type on lists
Lloyd Smith <lloyd.g.smith <at> gmail.com>
2005-04-02 22:41:19 GMT
2005-04-02 22:41:19 GMT
Hi everyone, I defined my own list datatype and then tried to declare it as an instance of type class Ord. However when I test it with Nil > Cons 1(Nil) I get an "ERROR - Control stack overflow" I am under the impression that the ord class defines default implementations of (<=), (>),(>=) so that I only have to supply the implementation of (<) shown below. Can some one tell me why this does not work the way I expect it to? My very own list data type > data List a = Nil > | Cons a (List a) > deriving (Show) Derive the equality type for list. > instance (Eq a) => Eq (List a) where > Nil == Nil = True > Nil == Cons y ys = False > Cons x xs == Nil = False > Cons x xs == Cons y ys = (x == y) && (xs == ys) Derive the ordered type for list > instance (Ord a) => Ord (List a) where > Nil < Nil = False > Nil < Cons y ys = True(Continue reading)
3 Apr 2005 01:12
Re: Ord type on lists
Lemmih <lemmih <at> gmail.com>
2005-04-02 23:12:45 GMT
2005-04-02 23:12:45 GMT
On Apr 3, 2005 12:41 AM, Lloyd Smith <lloyd.g.smith <at> gmail.com> wrote: > Hi everyone, I defined my own list datatype and then tried to declare > it as an instance of type class Ord. However when I test it with > > Nil > Cons 1(Nil) > I get an "ERROR - Control stack overflow" > > I am under the impression that the ord class defines default > implementations of (<=), (>),(>=) so that I only have to supply the > implementation of (<) shown below. Can some one tell me why this does > not work the way I expect it to? > > My very own list data type > > > data List a = Nil > > | Cons a (List a) > > deriving (Show) > > Derive the equality type for list. > > > instance (Eq a) => Eq (List a) where > > Nil == Nil = True > > Nil == Cons y ys = False > > Cons x xs == Nil = False > > Cons x xs == Cons y ys = (x == y) && (xs == ys) > > Derive the ordered type for list > > > instance (Ord a) => Ord (List a) where > > Nil < Nil = False(Continue reading)
3 Apr 2005 01:35
Re: Ord type on lists
Claus Reinke <claus.reinke <at> talk21.com>
2005-04-02 23:35:10 GMT
2005-04-02 23:35:10 GMT
> Hi everyone, I defined my own list datatype and then tried to declare > it as an instance of type class Ord. However when I test it with > > Nil > Cons 1(Nil) > I get an "ERROR - Control stack overflow" > > I am under the impression that the ord class defines default > implementations of (<=), (>),(>=) so that I only have to supply the > implementation of (<) shown below. Can some one tell me why this does > not work the way I expect it to? Now, whereever did that interpretation come from?-) The library docs are pretty clear about what constitutes a minimal definition: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t%3AOrd Have a closer look at Ord and its default definitions to see what happens in your case: http://www.haskell.org/onlinereport/basic.html#sect6.3.2 hth, claus
3 Apr 2005 05:04
Re: Ord type on lists
Lloyd Smith <lloyd.g.smith <at> gmail.com>
2005-04-03 03:04:03 GMT
2005-04-03 03:04:03 GMT
On Apr 2, 2005 4:12 PM, Lemmih <lemmih <at> gmail.com> wrote: > <=), (>) and (>=) are defined using 'compare', not (<). Write > 'compare' yourself and everything will be good. > But why don't you just derive Eq and Ord for List? I didn't even think of doing that. So I will say I did this way for pedagogical reasons(Continue reading)On Apr 2, 2005 4:35 PM, Claus Reinke <claus.reinke <at> talk21.com> wrote: > > Hi everyone, I defined my own list datatype and then tried to declare > > it as an instance of type class Ord. However when I test it with > > > > Nil > Cons 1(Nil) > > I get an "ERROR - Control stack overflow" > > > > I am under the impression that the ord class defines default > > implementations of (<=), (>),(>=) so that I only have to supply the > > implementation of (<) shown below. Can some one tell me why this does > > not work the way I expect it to? > > Now, whereever did that interpretation come from?-) Page 32 of Introduction to Functional Programming using Haskell. I guess thats what I deserve for trusting a dead trees
> > The library docs are pretty clear about what constitutes a minimal definition: > > http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t%3AOrd > > Have a closer look at Ord and its default definitions to see what happens in
3 Apr 2005 09:38
Data.Map
Gracjan Polak <gracjan <at> acchsh.com>
2005-04-03 07:38:11 GMT
2005-04-03 07:38:11 GMT
Hi all, As I tried to convert some of my code to newer libraries comming with GHC 6.4, I would like to share two things with you: I noticed that there are two functions missing: deleteList and insertList. The first one is easy with foldl: deleteList list map = foldl (flip Data.Map.delete) map list Second one is even shorter: insertList asclist map = union map (Data.Map.fromList asclist) Still I find both of them useful also execution speed seems to be of some concern here. Why generate temporary map just to join it with second in a moment? Isn't it slower? Also why Data.Map.lookup and Data.Map.findWithDefault? Why not lookupWithDefault? Besides, I really like new library :) -- -- Gracjan
3 Apr 2005 13:54
Re: Data.Map
Sebastian Sylvan <sebastian.sylvan <at> gmail.com>
2005-04-03 11:54:04 GMT
2005-04-03 11:54:04 GMT
On Apr 3, 2005 9:38 AM, Gracjan Polak <gracjan <at> acchsh.com> wrote: > insertList asclist map = union map (Data.Map.fromList asclist) How about: insertList :: (Ord a) => Map a b -> [(a, b)] -> Map a b insertList = foldr (uncurry insert) /S -- -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
3 Apr 2005 14:30
Re: Ord type on lists
Claus Reinke <claus.reinke <at> talk21.com>
2005-04-03 12:30:31 GMT
2005-04-03 12:30:31 GMT
> > Now, whereever did that interpretation come from?-) > Page 32 of Introduction to Functional Programming using Haskell. I > guess thats what I deserve for trusting a dead treesI don't have my copy at hand - the online errata http://web.comlab.ox.ac.uk/oucl/publications/books/functional/ mention some confusion regarding Ord on page 33, but perhaps you may want to add to that list of errors? cheers, claus even dead trees come with security patches these days;)
3 Apr 2005 16:24
Re: Data.Map
Gracjan Polak <gracjan <at> acchsh.com>
2005-04-03 14:24:32 GMT
2005-04-03 14:24:32 GMT
Sebastian Sylvan wrote: > On Apr 3, 2005 9:38 AM, Gracjan Polak <gracjan <at> acchsh.com> wrote: > > >>insertList asclist map = union map (Data.Map.fromList asclist) > > > How about: > > insertList :: (Ord a) => Map a b -> [(a, b)] -> Map a b > insertList = foldr (uncurry insert) > Is there any reason why foldr is better than foldl here? > > /S -- -- Gracjan
5 Apr 2005 18:37
Hugs CPP macros?
John Goerzen <jgoerzen <at> complete.org>
2005-04-05 16:37:07 GMT
2005-04-05 16:37:07 GMT
Hi, I need to use some conditional compilation in MissingH so that it can work with GHC 6.2, 6.4, and the old/new Hugs libraries. I found the appropriate macros to test for with ghc, but I can't for Hugs. The cpphs-hugs doesn't even define __HUGS__. It seems that only hugs-hc and ffihugs define that, but I don't need them. And where __HUGS__ is defined, it isn't defined with a version number, so it doesn't help me differentiate the Hugs versions. Any suggstions? Thanks, John
On Apr 2, 2005 4:35 PM, Claus Reinke <claus.reinke <at> talk21.com> wrote:
> > Hi everyone, I defined my own list datatype and then tried to declare
> > it as an instance of type class Ord. However when I test it with
> >
> > Nil > Cons 1(Nil)
> > I get an "ERROR - Control stack overflow"
> >
> > I am under the impression that the ord class defines default
> > implementations of (<=), (>),(>=) so that I only have to supply the
> > implementation of (<) shown below. Can some one tell me why this does
> > not work the way I expect it to?
>
> Now, whereever did that interpretation come from?-)
Page 32 of Introduction to Functional Programming using Haskell. I
guess thats what I deserve for trusting a dead trees
RSS Feed