Arjan van IJzendoorn | 1 Apr 2005 10:09
Picon

Re: Existential problem

Haha! I like the subject of this message :)
Lloyd Smith | 3 Apr 2005 00:41
Picon

Ord type on lists

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)

Lemmih | 3 Apr 2005 01:12
Picon
Gravatar

Re: Ord type on lists

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)

Claus Reinke | 3 Apr 2005 01:35

Re: Ord type on lists

> 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
Lloyd Smith | 3 Apr 2005 05:04
Picon

Re: Ord type on lists

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 :-)

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
(Continue reading)

Gracjan Polak | 3 Apr 2005 09:38

Data.Map


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
Sebastian Sylvan | 3 Apr 2005 13:54
Picon

Re: Data.Map

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
Claus Reinke | 3 Apr 2005 14:30

Re: Ord type on lists

> > 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 :-)

I 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;)
Gracjan Polak | 3 Apr 2005 16:24

Re: Data.Map

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
John Goerzen | 5 Apr 2005 18:37
Favicon

Hugs CPP macros?

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

Gmane