Is there a library function to take a list of Strings and return a list of
ints showing how many times each String occurs in the list.
So for example:
[egg, egg, cheese] would return [2,1]
I couldn't find anything on a search, or anything in the librarys.
Thanks BH.
--
View this message in
BH,
Is there a library function to take a list of Strings and return a
list of
ints showing how many times each String occurs in the list.
So for example:
[egg, egg, cheese] would return [2,1]
freq xs = map length (group xs)
HTH,
Stefan
___
On 17/10/2007, Big_Ham [EMAIL PROTECTED] wrote:
Is there a library function to take a list of Strings and return a list of
ints showing how many times each String occurs in the list.
So for example:
[egg, egg, cheese] would return [2,1]
I couldn't find anything on a search, or anything in
On 17/10/2007, Dougal Stanton [EMAIL PROTECTED] wrote:
No, but it is also trivial to create, with the 'group' function in
Data.List. I'll stop there though, cos this could be a homework
question.
It's just occurred to me that answering questions like these is a bit
like the prisoner's
you are indeed right Peter, that's what I was after, the frequency regardless
of elements. It also doesn't matter if it outputs them as tuples, or as a
separate list on their own because each value would belong to the first
occurance of that element if you seem what I mean, so you could still
Dougal Stanton wrote:
It's just occurred to me that answering questions like these is a bit
like the prisoner's dilemma...
There's no way to win! :-)
Yes there is. Just mention the following wiki page
as part of your answer:
http://haskell.org/haskellwiki/Homework_help
-Yitz
On 10/17/07, Peter Verswyvelen [EMAIL PROTECTED] wrote:
So in that case, the result should be a list of ordered pairs like:
[(egg, 2), (cheese, 1)]. Or a pair of two lists, like ([egg,
cheese), (2,1)]. Otherwise you would not know which frequency belongs
to which element?
However, I suspect
I'm a newbie here, so I'm not sure about my reply, but I think this is
not the answer to his question.
freq [egg, egg, cheese] indeed returns [2,1]
but
freq [egg, cheese, egg] returns [1,1,1]
BH just mentioned he needed the frequenty of elements in the list,
independent of their order.
So
Peter Verswyvelen wrote:
However, I suspect the experts here will be able to make that much
shorter and more efficient (maybe using Data.Map?)
That makes it difficult to respond. I am definitely not
claiming to be an expert. For one thing, my name
is not Simon. But I'll say something anyway,
Nice!!! As I'm learning Arrows now, this is really useful :-)
Stuart Cook wrote:
import Control.Arrow
import Data.List
freqs = map (head length) . group . sort
I have used this function quite a few times already.
Stuart
___
Haskell-Cafe
I wrote:
When you can assume Ord, the standard solution
is, as you suggest, something like...
Oops, sorry, doesn't typecheck. Here it is corrected:
import qualified Data.Map as M
import Data.List
histogram = M.toList . foldl' (\m x - M.insertWith' (+) x 1 m) M.empty
This should work
The problem there is that nub is O(n^2).
Is there a place where one can look up the complexity of Standard
Libraries functions ?
E.D
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Is there a place where one can look up the complexity of Standard
Libraries functions ?
No. Some modules have it in their Haddock docs.
Most don't.
But the source code is available. :)
-Yitz
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
Oh, why didn't you say you were learning Arrows? Then why not
freqs = sort group map (head length)
So much more readable, don't you think? ;)
Either way, if you run into the dreaded monomorphism restriction:
Ambiguous type variable `a' in the constraint:
`Ord a' arising from use
14 matches
Mail list logo