, Chad; haskell@haskell.org
Subject: Re: [Haskell] stack overflow - nonobvious thunks?
Dean's version certainly seems the neatest, but just for interest you
can also do it with a cps fold instead of foldl' too:
table xs = assocs $! cpsfold f empty xs
where
f x m k = case Map.lookup x m
Title: RE: [Haskell] stack overflow - nonobvious
thunks?
The following version seems to do the trick (and still remain
quite readable). It worked for 1 as well.
import Data.Map as Map
import System.Random
import Data.List (foldl')
table :: (Ord a) = [a] - [(a,Int)]
table xs
Dean's version certainly seems the neatest, but just for interest you
can also do it with a cps fold instead of foldl' too:
table xs = assocs $! cpsfold f empty xs
where
f x m k = case Map.lookup x m of
Just v - v `seq` (k $ Map.adjust (+1) x m)
Nothing -
On Wednesday 27 Jul 2005 10:19 pm, Scherrer, Chad wrote:
Adrian, Does your AVL library have an insertWith'-type function
mentioned by Udo?
I haven't followed this too closely, but I did try to ensure that
Data.Tree.AVL provides all the strictness control users will need
in practice.
Basically
Title: stack overflow - nonobvious thunks?
I'm trying to write a function to build a table of values from a list. Here's my current attempt:
table :: (Ord a) = [a] - [(a,Int)]
table xs = Map.assocs $! foldl' f Map.empty xs
where
f m x = Map.insertWith (+) x 1 m
The ($!) and the
Scherrer, Chad wrote:
f m x = Map.insertWith (+) x 1 m
insertWith is inserting the nonobvious thunks. Internally it applies
(+) to the old value and the new one, producing a thunk. There is no
place you could put a seq or something to force the result. You
basically need insertWith',
for very large n like this.
-Chad
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Wednesday, July 27, 2005 11:02 AM
To: Scherrer, Chad
Cc: haskell@haskell.org
Subject: Re: [Haskell] stack overflow - nonobvious thunks?
Scherrer, Chad wrote:
f m x