Thread: Haskell
View Single Post
Old Jul 22nd, 2006, 2:21 PM   #1
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Haskell

Does anyone have any experience with Haskell?

I've been trying to learn the language (based on the principle that if you can learn Haskell, you can learn anything), and I've been writing a number of small programs to help me learn.

One of the programs is a module that implements a bag data structure (similar to a set, but keeps track of the amounts of each item). I've been trying to construct a function to give me a sub-bag of the N most common elements, but I have yet to have much luck. Here's what I have so far:
module Bag where

import Data.Map as Map

type Bag a = Map a Int

fromList :: Ord a => [a] -> Bag a
fromList []     = Map.empty
fromList (x:xs) = Map.insertWith (+) x 1 (Bag.fromList xs)

mostCommon :: Int -> Bag a -> Bag a
{- How to implement this function ... -}
Arevos is offline   Reply With Quote