need help

need help

Post by vale » Sat, 16 Oct 2004 05:35:14


hello, im new with haskell and im trying to write a function that gives a
list of lists of same objects if u write a list of all the objects.for
example writing
fun [a,a,b,c,a,b] will give back [[a,a,a],[b,b],[c]] any ideas?
 
 
 

need help

Post by Vincenzo C » Sat, 16 Oct 2004 05:47:04


If you look at Prelude.List I bet you'll find a couple of useful functions
to do what you need, perhaps start by sorting the list :)

V.

--
Please notice that my e-mail address has been altered to prevent spam

 
 
 

need help

Post by vale » Sat, 16 Oct 2004 09:15:11

i tried on the list library but havent find something useful.i dont want to
sort the list but just to produce a list of lists with every list having
the same items. i.e from [a,a,b] to get [[a,a],[b]].

ive done this so far but cant finish it

list :: [a] -> [[a]]
list [] = []
list (h:[]) = [[h]]
list(h:t)=...

thank u
 
 
 

need help

Post by mwotto » Sat, 16 Oct 2004 10:22:45

On Thu, 14 Oct 2004 20:15:11 -0400, valez posted:

I think you need to make it a bit clearer what you're trying to do.
would ['a','b','a'] go to [['a','a'],['b']], for instance, or are you
just trying to group them locally?

mrak

--
realise your life was only bait for a bigger fish
-- aesop rock
 
 
 

need help

Post by Adrian He » Sat, 16 Oct 2004 13:04:04


Actually I think valez aims were clear from his first post.
One easy way is with my own pet AVL library:-)

http://www.yqcomputer.com/ ~ahey/HLibs/Data.Tree.AVL/

(Probably only installs properly with ghc at the moment).

The following does the trick I think..

import Data.Tree.AVL

fun :: Ord a => [a] -> [[a]]
fun as = [ replicate n a
| (a,n) <- asListL (asTreeKeyWith' (+) [(a,1) | a <- as])
]

I guess you could do something similar with Data.FiniteMap too.

I'll leave figuring out how it works as an exercise for valez :-)
(newbie hint: uses two list comprehensions)

Regards
Adrian Hey
 
 
 

need help

Post by Tomasz Zie » Sat, 16 Oct 2004 17:20:43


A bit too complicated for my taste. Also, you would equalize externally
unequivalent Eq-ual elements, if you know what I mean ;)

Here's another hint: fun can be written as

fun l = f1 (f2 l)

where f1 and f2 are some function from module List.

Best regards,
Tom

--
.signature: Too many levels of symbolic links
 
 
 

need help

Post by Tomasz Zie » Sat, 16 Oct 2004 17:21:48


or is that inequivalent?

best regards,
tomasz

--
.signature: Too many levels of symbolic links
 
 
 

need help

Post by Tomasz Zie » Sat, 16 Oct 2004 17:23:37


observationally inequivalent

Damn, that's what you get when you try to look smarter than you really
are ;)

Best regards,
Tom

--
.signature: Too many levels of symbolic links
 
 
 

need help

Post by mwotto » Sat, 16 Oct 2004 18:18:50

On Fri, 15 Oct 2004 08:23:37 +0000 (UTC), Tomasz Zielonka posted:


well, now that we've thoroughly confused the original poster... :)

mrak

--
realise your life was only bait for a bigger fish
-- aesop rock
 
 
 

need help

Post by Adrian He » Sun, 17 Oct 2004 22:21:03


I think I know what you mean, but I don't think it's a problem. AFAIK for
all predefined or derived instances of Ord and Eq the following
holds..
(x `compare` y = EQ) implies (x == y = True)
and..
(x == y = True) implies (x = y)

I always thought that was the intended semantics anyway.


Yes, I think this solution must be what whoever set this problem
had in mind. (Though if it uses the standard H98 sort I wouldn't
advocate it's use in real programs :-)

Regards
Adrian Hey
 
 
 

need help

Post by Adrian He » Mon, 18 Oct 2004 01:53:54


I've just had a look at the Data.List source code and it seems someone
has changed the sort to something much better than that defined in the
H98 standard libraries. (I don't want to spread misinformation on this,
hence this post).

Regards
Adrian Hey
 
 
 

need help

Post by Tomasz Zie » Mon, 18 Oct 2004 02:19:55


From The Haskell 98 Report:

8 Standard Prelude

In this chapter the entire Haskell Prelude is given. It constitutes a
specification for the Prelude. Many of the definitions are written with
clarity rather than efficiency in mind, and it is not required that the
specification be implemented as shown here.

As you can see there is no obligation to use the implementation from The
Report.

Best regards,
Tom

--
.signature: Too many levels of symbolic links
 
 
 

need help

Post by Adrian He » Tue, 19 Oct 2004 01:19:15


You're gonna think I'm nit-picking, but sort isn't part of the prelude, so
these words don't apply :-)

You'd think that similar words would have been used for the standard
libraries too, but I can't find them. In fact, IIRC someone recently
stated on one of the Haskell mailing lists that to be H98 compliant,
the standard libraries had to be implemented exactly as given in the
report. Unfortunately I've forgotten who it was :-(

But seeing as we're actually talking about sort from Data.List I guess
none of this matters really (H98 standard is N/A anyway).

Though why the Haskell folk chose to include such an obviously crap
sort algorithm in the standard in the first place has always puzzled
me (and a many other folk too I think). Even if there was an unstated
assumption that implementors would not be foolish enough to use it
in practice, it's appearance is just plain embarrassing IMO :-)

Regards
Adrian Hey