Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Re: in which monad am I? (Francesco Guerrieri)
   2. Re:  in which monad am I? (Francesco Guerrieri)
   3.  Re: Re: in which monad am I? (Maciej Piechotka)
   4.  Unexpected Space Leak (despite using     external-sort) (Peter Green)
   5. Re:  Unexpected Space Leak (despite       using   external-sort)
      (Stephen Blackheath [to Haskell-Beginners])
   6. Re:  Unexpected Space Leak (despite using external-sort)
      (Peter Green)


----------------------------------------------------------------------

Message: 1
Date: Sun, 3 Jan 2010 23:44:48 +0100
From: Francesco Guerrieri <[email protected]>
Subject: Re: [Haskell-beginners] Re: in which monad am I?
To: Maciej Piechotka <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Jan 3, 2010 at 11:21 PM, Maciej Piechotka <[email protected]> wrote:
> On Sun, 2010-01-03 at 22:38 +0100, Francesco Guerrieri wrote:
>> My question is: how do you know in which monad you are computing at a
>> given time?

> 'In which monad' question is the same as 'can type system be confused'.

I like this way of rephrasing it. Rahul's earlier answer led me to it, too.

> Except for syntax sugar (do) monads are nothing special in Haskell. Not
> more special then Read or Show classes.
>
> For example (for type system):
>
>> f :: [String] -> [String]
>> f = map show . map read
>>
>> main = interact (unlines . f . lines)
>
> test.lhs:6:21:
>    Ambiguous type variable `a' in the constraints:
>      `Read a' arising from a use of `read' at test.lhs:6:21-24
>      `Show a' arising from a use of `show' at test.lhs:6:10-13
>    Probable fix: add a type signature that fixes these type variable(s)

Ok, I understand it this way:
f takes a list of string, maps "read" to it and maps "show"
(converting them to their string representation) to the resulting
list. f  never explicitly bothers with the type to which the strings
were "read", since it shows them asap. BUT if the compiler is to pick
an implementation of read, the type must be known.

>
> With monad it is harder (as monad is one-way) but it is possible to do
> it:
>
>> extract $ liftM (+1) (return 0)
>
> results in:
>
>    Ambiguous type variable `f' in the constraints:
>      `Monad f' arising from a use of `liftM' at <interactive>:1:10-30
>      `Copointed f'
>        arising from a use of `extract' at <interactive>:1:0-6
>    Probable fix: add a type signature that fixes these type variable(s)
>
> We only know that we operate on some monad which is copointed. But we
> have no idea what is it. However in 99% of cases we don't have to.
>
> (You man notice the signature of extract is - extract :: (Copointed f)
> => f a -> a so the problem is when we remove type [type does not occures
> on RHS of last ->])


Ok, you're losing me a bit with the "copointed f".
Could you please clarify further the example? I assume that extract is
the "inverse" of liftM. I have not yet found it and searching for
"haskell extract monad" doesn't find a specific reference link. If
extract "unlifts" a value out of a Monad I think that it cannot be
defined for every monad (you cannot get out from the IO monad....
right?). In this sense you are first lifting and then unlifting like
earlier you were first reading and then showing...? and so in
principle you are "monad agnostic" but the type system is puzzled
because it doesn't know to which monad you want to lift?

Thanks,
Francesco


------------------------------

Message: 2
Date: Mon, 4 Jan 2010 00:02:41 +0100
From: Francesco Guerrieri <[email protected]>
Subject: Re: [Haskell-beginners] in which monad am I?
To: Felipe Lessa <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Jan 3, 2010 at 11:30 PM, Felipe Lessa <[email protected]> wrote:
> On Sun, Jan 03, 2010 at 10:38:28PM +0100, Francesco Guerrieri wrote:
>> But doesn't ever happen that poor liftM be confused? and do poor
>> haskell programmers never feel the need to explicitly state to which
>> monad they "wish to lift" ?
>
> If by "explicitly state to which monad" you mean specializing
> liftM or fmap, then the answer is no.  It is not uncommon to see
>
>  fmap (fmap function)
>
> being used to map two layers of structures.  If by stating you
> mean giving a type signature, then yes, it is a good practice to
> give type signatures to all top-level definitions.

I was thinking to type signatures :-)
By the way, you all refer to top-level definition. But in the map show
. map read example, what I see at the top-level is
[String] -> [String]. This probably means that my understanding of
what is a top-level definition is faulty...

> The compiler never gets "confused".  The only thing that may
> happen is having more than one possible type that fulfills your
> needs.  The classical example is Num overloading:

This is a very clear example, thanks!

Francesco


------------------------------

Message: 3
Date: Mon, 04 Jan 2010 00:14:27 +0100
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Re: in which monad am I?
To: [email protected]
Message-ID: <1262560467.13413.29.ca...@picard>
Content-Type: text/plain; charset="UTF-8"

On Sun, 2010-01-03 at 23:44 +0100, Francesco Guerrieri wrote:
> On Sun, Jan 3, 2010 at 11:21 PM, Maciej Piechotka <[email protected]> 
> wrote:
> > Except for syntax sugar (do) monads are nothing special in Haskell. Not
> > more special then Read or Show classes.
> >
> > For example (for type system):
> >
> >> f :: [String] -> [String]
> >> f = map show . map read
> >>
> >> main = interact (unlines . f . lines)
> >
> > test.lhs:6:21:
> >    Ambiguous type variable `a' in the constraints:
> >      `Read a' arising from a use of `read' at test.lhs:6:21-24
> >      `Show a' arising from a use of `show' at test.lhs:6:10-13
> >    Probable fix: add a type signature that fixes these type variable(s)
> 
> Ok, I understand it this way:
> f takes a list of string, maps "read" to it and maps "show"
> (converting them to their string representation) to the resulting
> list. f  never explicitly bothers with the type to which the strings
> were "read", since it shows them asap. BUT if the compiler is to pick
> an implementation of read, the type must be known.
> 

Yes. It knows that he should call some read and some show functions but
have no idea which function.

> >
> > With monad it is harder (as monad is one-way) but it is possible to do
> > it:
> >
> >> extract $ liftM (+1) (return 0)
> >
> > results in:
> >
> >    Ambiguous type variable `f' in the constraints:
> >      `Monad f' arising from a use of `liftM' at <interactive>:1:10-30
> >      `Copointed f'
> >        arising from a use of `extract' at <interactive>:1:0-6
> >    Probable fix: add a type signature that fixes these type variable(s)
> >
> > We only know that we operate on some monad which is copointed. But we
> > have no idea what is it. However in 99% of cases we don't have to.
> >
> > (You man notice the signature of extract is - extract :: (Copointed f)
> > => f a -> a so the problem is when we remove type [type does not occures
> > on RHS of last ->])
> 
> 
> Ok, you're losing me a bit with the "copointed f".

It's one more strange class. The definition (from category-extras
package if you need to know):

class Copointed f where
  extract :: f a -> a

Sorry if I mess a bit. You got the main point so read further only if
you want to know a bit about copointed

> Could you please clarify further the example? I assume that extract is
> the "inverse" of liftM.

It's rather inverse of return. However it is unlikely that you'll ever
use it (at least - I haven't and many programs/libraries have not). It
was just an example.

> I have not yet found it and searching for
> "haskell extract monad" doesn't find a specific reference link.

http://hackage.haskell.org/packages/archive/category-extras/0.53.5/doc/html/Control-Functor-Pointed.html#t%3ACopointed

Generally use hayoo[1] (hoogle stop working for me lately) inserting for
example 'extract'. 

[1] http://holumbus.fh-wedel.de/hayoo/hayoo.html

> If
> extract "unlifts" a value out of a Monad I think that it cannot be
> defined for every monad

Yest. Hence we know that it is monad which is copointed. I.e. we look
for such m that (Copointed m, Monad m) - m that it member of both
classes.

> (you cannot get out from the IO monad....
> right?).

Well. You can get out of IO monad. But you shouldn't (it creates lot's
of problems). But generally you cannot.

Imagine extractM :: Monad m => m a -> a

What would be extractM []

> In this sense you are first lifting and then unlifting like
> earlier you were first reading and then showing...? and so in
> principle you are "monad agnostic" but the type system is puzzled
> because it doesn't know to which monad you want to lift?
> 

Yes. However as long you're in Functor/Monad/Applicative range you are
quite safe as they are like Alcatraz - no escape (see however PS).

However Identity is both monad and Copointed (it is somewhere defined)

> newtype Identity a = Identity a
> instance Monad Identity where
>   (Identity v) >>= f   = f v
>   return               = Identity
> instance Copointed Identity where
>   extract (Identity v) = v

So:

> extract $ (liftM (+1) (return 0) :: Identity Int)

will work

> Thanks,
> Francesco

Regards

PS. Possibly simpler example:

> liftNull :: (Monad m, Monad n) => m () -> n ()
> liftNull _ = return ()

> liftNull $ liftM (+1) (return 0)




------------------------------

Message: 4
Date: Mon, 04 Jan 2010 09:35:04 +0800
From: Peter Green <[email protected]>
Subject: [Haskell-beginners] Unexpected Space Leak (despite using
        external-sort)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes

I am using the external-sort package to sort my output in the program  
below. I made this choice because my output dataset [[Int]] can be  
large (e.g. >3M items, each [Int]).

What my program does:

(1) Reads in a file containing 'compressed lists' which look like so:

8+12,11,7+13,10
1+2+3,1+9,3+6,4
.
.

One compressed list per line. These compressed lists are parsed to  
become [[[Int]]]

[[[8,12],[11],[7,13],[10]],
  [[1,2,3],[1,9],[3,6],[4]],
.
.
]

Generally files of compressed lists have lengths of ~10,000 lines.

(2) Compressed lists are exploded to [[int]] via concatMap Cartesian  
Product over [[[Int]]], so we end up with [[Int]]

[[8, 11, 7, 10],
  [8, 11, 13, 10],
  [12, 11, 7, 10],
  [12, 11, 13, 10],
  [1, 1, 3, 4],
  .
  .
  [3, 9, 6, 4]]

These 'exploded lists are' *much* longer than the input lists. It's  
common for them to have >100K [Int] members, or even >1M [Int] members.

(3) This [[Int]] data must be sorted in to lexicographic order and  
output as CSV data: i.e. output should be:

1,1,3,4
3,9,6,4
8,11,7,10
.
.
12,11,13,10

There is no way to avoid the necessity of sorting the final product. I  
can *not* take advantage of any structure in the input data to avoid  
this sort stage. e.g. sorting the (smaller) input compressed lists  
*will not* obviate the need to lex sort the (larger) final output.

The program listing below works correctly, but *is not* space- 
efficient. In fact it is *less* space-efficient than the equivalent   
program using Prelude sort!

e.g. with one data set which explodes to ~3.7M I am seeing 639MB of  
total memory use.

I am sure externalSort is not at fault because:
(a) I'm the Newbie.
(b) ExternalSort.bin is 'only' 228MB in size. (Expected to be not  
small because sorting a [[Int]] with 3.7M [Int] members.)

I'm sure it must be something I'm doing wrong elsewhere which is  
causing the entire output data list to be read into memory.

What I really want to happen is:

(1) Data lazily read in from stdin and lazily parsed
(2) Data lazily sucked out to disk and strictly sorted by externalSort  
(but no big deal since this happening on disk)
(3) Data lazily sucked back out of externalSort, lazily formatted for  
output, and lazily written to stdout.

So memory usage should hopefully never go over (say 10MB). But in fact  
I'm using 638MB. So somewhere in the code below I must be doing  
something very wrong.

I hope someone can tell me what I am doing wrong here.

TIA!


import Algorithms.ExternalSort
import Data.List.Split (splitOn)
import System.IO

-- Cartesian Product over a List of Lists
-- Http://www.cs.nott.ac.uk/~gmh/sudoku.lhs
-- cp [[1,2],[3],[4,5,6]] --> [[1,3,4],[1,3,5],[1,3,6],[2,3,4],[2,3,5], 
[2,3,6]]
cp          :: [[a]] -> [[a]]
cp []       =  [[]]
cp (xs:xss) =  [y:ys | y <- xs, ys <- cp xss]

-- fromCSV ["8+12,11,7+13,10", "1+2+3,1+9,3+6,4"] -->
-- [[[8,12],[11],[7,13],[10]],[[1,2,3],[1,9],[3,6],[4]]]
fromCSV :: [String] -> [[[Int]]]
fromCSV = map parseOneLine
     where parseOneLine = map parseGroup . splitOn ","
               where parseGroup = map read . splitOn "+"

-- explode [[[1,2],[3],[4,5,6]], [[1, 2], [14,15], [16]]] --> [[1,3,4], 
[1,3,5],
-- [1,3,6],[2,3,4],[2,3,5],[2,3,6],[1,14,16],[1,15,16],[2,14,16], 
[2,15,16]]
explode :: [[[a]]] -> [[a]]
explode =  concatMap cp

-- toSingles "8+12,11,7+13,10\n1+2+3,1+9,3+6,4" -->
-- [[8,11,7,10],[8,11,13,10],[12,11,7,10],[12,11,13,10],[1,1,3,4], 
[1,1,6,4],
--  [1,9,3,4],[1,9,6,4],[2,1,3,4],[2,1,6,4],[2,9,3,4],[2,9,6,4], 
[3,1,3,4],
--  [3,1,6,4],[3,9,3,4],[3,9,6,4]]
toSingles :: String -> [[Int]]
toSingles = explode . fromCSV . lines

-- toCSV [8,11,7,10,12] --> "8,11,7,10,12"
toCSV :: (Show a) => [a] -> String
toCSV = tail . init . show

main = do
   getContents >>= externalSort . toSingles >>= mapM_ (putStrLn . toCSV)



------------------------------

Message: 5
Date: Mon, 04 Jan 2010 15:43:13 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: Re: [Haskell-beginners] Unexpected Space Leak (despite using
        external-sort)
To: Peter Green <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Peter,

I can't see anything in your code that stops it being lazy, and I also
read the source for external-sort.  It all looks OK except for one bit.
 There's even a comment that says "It would be better if I changed Ord
for blocks to only check the first element."  Looks like it's comparing
the whole list instead of the first element when merging the lists, and
so it's a bit difficult to guess how much it is evaluating of each list.
 If there are many repeated elements, it could be a lot.

You could try fixing that.  You could hack kMerge using a newtype and an
Ord instance (since Splay takes Ord instances).


Steve

Peter Green wrote:
> I am using the external-sort package to sort my output in the program
> below. I made this choice because my output dataset [[Int]] can be large
> (e.g. >3M items, each [Int]).
> 
> What my program does:
> 
> (1) Reads in a file containing 'compressed lists' which look like so:
> 
> 8+12,11,7+13,10
> 1+2+3,1+9,3+6,4
> .
> .
> 
> One compressed list per line. These compressed lists are parsed to
> become [[[Int]]]
> 
> [[[8,12],[11],[7,13],[10]],
>  [[1,2,3],[1,9],[3,6],[4]],
> .
> .
> ]
> 
> Generally files of compressed lists have lengths of ~10,000 lines.
> 
> (2) Compressed lists are exploded to [[int]] via concatMap Cartesian
> Product over [[[Int]]], so we end up with [[Int]]
> 
> [[8, 11, 7, 10],
>  [8, 11, 13, 10],
>  [12, 11, 7, 10],
>  [12, 11, 13, 10],
>  [1, 1, 3, 4],
>  .
>  .
>  [3, 9, 6, 4]]
> 
> These 'exploded lists are' *much* longer than the input lists. It's
> common for them to have >100K [Int] members, or even >1M [Int] members.
> 
> (3) This [[Int]] data must be sorted in to lexicographic order and
> output as CSV data: i.e. output should be:
> 
> 1,1,3,4
> 3,9,6,4
> 8,11,7,10
> .
> .
> 12,11,13,10
> 
> There is no way to avoid the necessity of sorting the final product. I
> can *not* take advantage of any structure in the input data to avoid
> this sort stage. e.g. sorting the (smaller) input compressed lists *will
> not* obviate the need to lex sort the (larger) final output.
> 
> The program listing below works correctly, but *is not* space-efficient.
> In fact it is *less* space-efficient than the equivalent  program using
> Prelude sort!
> 
> e.g. with one data set which explodes to ~3.7M I am seeing 639MB of
> total memory use.
> 
> I am sure externalSort is not at fault because:
> (a) I'm the Newbie.
> (b) ExternalSort.bin is 'only' 228MB in size. (Expected to be not small
> because sorting a [[Int]] with 3.7M [Int] members.)
> 
> I'm sure it must be something I'm doing wrong elsewhere which is causing
> the entire output data list to be read into memory.
> 
> What I really want to happen is:
> 
> (1) Data lazily read in from stdin and lazily parsed
> (2) Data lazily sucked out to disk and strictly sorted by externalSort
> (but no big deal since this happening on disk)
> (3) Data lazily sucked back out of externalSort, lazily formatted for
> output, and lazily written to stdout.
> 
> So memory usage should hopefully never go over (say 10MB). But in fact
> I'm using 638MB. So somewhere in the code below I must be doing
> something very wrong.
> 
> I hope someone can tell me what I am doing wrong here.
> 
> TIA!
> 
> 
> import Algorithms.ExternalSort
> import Data.List.Split (splitOn)
> import System.IO
> 
> -- Cartesian Product over a List of Lists
> -- Http://www.cs.nott.ac.uk/~gmh/sudoku.lhs
> -- cp [[1,2],[3],[4,5,6]] -->
> [[1,3,4],[1,3,5],[1,3,6],[2,3,4],[2,3,5],[2,3,6]]
> cp          :: [[a]] -> [[a]]
> cp []       =  [[]]
> cp (xs:xss) =  [y:ys | y <- xs, ys <- cp xss]
> 
> -- fromCSV ["8+12,11,7+13,10", "1+2+3,1+9,3+6,4"] -->
> -- [[[8,12],[11],[7,13],[10]],[[1,2,3],[1,9],[3,6],[4]]]
> fromCSV :: [String] -> [[[Int]]]
> fromCSV = map parseOneLine
>     where parseOneLine = map parseGroup . splitOn ","
>               where parseGroup = map read . splitOn "+"
> 
> -- explode [[[1,2],[3],[4,5,6]], [[1, 2], [14,15], [16]]] -->
> [[1,3,4],[1,3,5],
> -- [1,3,6],[2,3,4],[2,3,5],[2,3,6],[1,14,16],[1,15,16],[2,14,16],[2,15,16]]
> explode :: [[[a]]] -> [[a]]
> explode =  concatMap cp
> 
> -- toSingles "8+12,11,7+13,10\n1+2+3,1+9,3+6,4" -->
> --
> [[8,11,7,10],[8,11,13,10],[12,11,7,10],[12,11,13,10],[1,1,3,4],[1,1,6,4],
> --  [1,9,3,4],[1,9,6,4],[2,1,3,4],[2,1,6,4],[2,9,3,4],[2,9,6,4],[3,1,3,4],
> --  [3,1,6,4],[3,9,3,4],[3,9,6,4]]
> toSingles :: String -> [[Int]]
> toSingles = explode . fromCSV . lines
> 
> -- toCSV [8,11,7,10,12] --> "8,11,7,10,12"
> toCSV :: (Show a) => [a] -> String
> toCSV = tail . init . show
> 
> main = do
>   getContents >>= externalSort . toSingles >>= mapM_ (putStrLn . toCSV)
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
> 


------------------------------

Message: 6
Date: Mon, 04 Jan 2010 13:19:27 +0800
From: Peter Green <[email protected]>
Subject: Re: [Haskell-beginners] Unexpected Space Leak (despite using
        external-sort)
To: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes


On 04/01/2010, at 10:43 AM, Stephen Blackheath [to Haskell-Beginners]  
wrote:

> Peter,
>
> I can't see anything in your code that stops it being lazy, and I also
> read the source for external-sort.  It all looks OK except for one  
> bit.
> There's even a comment that says "It would be better if I changed Ord
> for blocks to only check the first element."  Looks like it's  
> comparing
> the whole list instead of the first element when merging the lists,  
> and
> so it's a bit difficult to guess how much it is evaluating of each  
> list.
> If there are many repeated elements, it could be a lot.
>
> You could try fixing that.  You could hack kMerge using a newtype  
> and an
> Ord instance (since Splay takes Ord instances).
>
>
> Steve

Thanks Steve. I think I understand your point. It seems like external- 
sort needs patching to make block comparison more efficient for  
everyone and that my case is just particularly extreme/degenerate.  
Just right now, I'm not yet confident enough about my Haskell  
abilities to go and create a new type for 'block' with more efficient  
Ord and then modify the rest of the external-sort code accordingly.  
Posted program was the 2nd Haskell prog I've every written. I'll get  
there in time, though :).

I wonder if there is another way of making my code work better with  
the existing externalSort function:

I am sorting [[Int]], an example of which might be:

[[8, 11, 7, 10],
[8, 11, 13, 10],
[12, 11, 7, 10],
[12, 11, 13, 10],
[1, 1, 3, 4],
.
.
[3, 9, 6, 4]]

I need numerical lex sorting, so I can't just map directly to  
ByteString as this would result in "12" collating before "1", etc.

I know that each sublist I am sorting never contains an integer  
greater than 20. I could encode each sublist as a ByteString where 1  
maps to a, 2 maps to B, 3, maps to C, etc.

So would have [1,1,3,4] -> "aacd" (ByteString), etc.

Hopefully Ord comparisons between ByteStrings are much less expensive  
than comparisons between instances of  [Int] and I will see nicer  
behaviour since external-sort does work as advertised on lists of  
millions of Ints?

So I could sort these, then map back to underlying Int data when I  
output. Schwartzian transform type stuff.

Anyway, I'll give it a go.


> Peter Green wrote:
>> I am using the external-sort package to sort my output in the program
>> below. I made this choice because my output dataset [[Int]] can be  
>> large
>> (e.g. >3M items, each [Int]).
>>
>> What my program does:
>>
>> (1) Reads in a file containing 'compressed lists' which look like so:
>>
>> 8+12,11,7+13,10
>> 1+2+3,1+9,3+6,4
>> .
>> .
>>
>> One compressed list per line. These compressed lists are parsed to
>> become [[[Int]]]
>>
>> [[[8,12],[11],[7,13],[10]],
>> [[1,2,3],[1,9],[3,6],[4]],
>> .
>> .
>> ]
>>
>> Generally files of compressed lists have lengths of ~10,000 lines.
>>
>> (2) Compressed lists are exploded to [[int]] via concatMap Cartesian
>> Product over [[[Int]]], so we end up with [[Int]]
>>
>> [[8, 11, 7, 10],
>> [8, 11, 13, 10],
>> [12, 11, 7, 10],
>> [12, 11, 13, 10],
>> [1, 1, 3, 4],
>> .
>> .
>> [3, 9, 6, 4]]
>>
>> These 'exploded lists are' *much* longer than the input lists. It's
>> common for them to have >100K [Int] members, or even >1M [Int]  
>> members.
>>
>> (3) This [[Int]] data must be sorted in to lexicographic order and
>> output as CSV data: i.e. output should be:
>>
>> 1,1,3,4
>> 3,9,6,4
>> 8,11,7,10
>> .
>> .
>> 12,11,13,10
>>
>> There is no way to avoid the necessity of sorting the final  
>> product. I
>> can *not* take advantage of any structure in the input data to avoid
>> this sort stage. e.g. sorting the (smaller) input compressed lists  
>> *will
>> not* obviate the need to lex sort the (larger) final output.
>>
>> The program listing below works correctly, but *is not* space- 
>> efficient.
>> In fact it is *less* space-efficient than the equivalent  program  
>> using
>> Prelude sort!
>>
>> e.g. with one data set which explodes to ~3.7M I am seeing 639MB of
>> total memory use.
>>
>> I am sure externalSort is not at fault because:
>> (a) I'm the Newbie.
>> (b) ExternalSort.bin is 'only' 228MB in size. (Expected to be not  
>> small
>> because sorting a [[Int]] with 3.7M [Int] members.)
>>
>> I'm sure it must be something I'm doing wrong elsewhere which is  
>> causing
>> the entire output data list to be read into memory.
>>
>> What I really want to happen is:
>>
>> (1) Data lazily read in from stdin and lazily parsed
>> (2) Data lazily sucked out to disk and strictly sorted by  
>> externalSort
>> (but no big deal since this happening on disk)
>> (3) Data lazily sucked back out of externalSort, lazily formatted for
>> output, and lazily written to stdout.
>>
>> So memory usage should hopefully never go over (say 10MB). But in  
>> fact
>> I'm using 638MB. So somewhere in the code below I must be doing
>> something very wrong.
>>
>> I hope someone can tell me what I am doing wrong here.
>>
>> TIA!
>>
>>
>> import Algorithms.ExternalSort
>> import Data.List.Split (splitOn)
>> import System.IO
>>
>> -- Cartesian Product over a List of Lists
>> -- Http://www.cs.nott.ac.uk/~gmh/sudoku.lhs
>> -- cp [[1,2],[3],[4,5,6]] -->
>> [[1,3,4],[1,3,5],[1,3,6],[2,3,4],[2,3,5],[2,3,6]]
>> cp          :: [[a]] -> [[a]]
>> cp []       =  [[]]
>> cp (xs:xss) =  [y:ys | y <- xs, ys <- cp xss]
>>
>> -- fromCSV ["8+12,11,7+13,10", "1+2+3,1+9,3+6,4"] -->
>> -- [[[8,12],[11],[7,13],[10]],[[1,2,3],[1,9],[3,6],[4]]]
>> fromCSV :: [String] -> [[[Int]]]
>> fromCSV = map parseOneLine
>>    where parseOneLine = map parseGroup . splitOn ","
>>              where parseGroup = map read . splitOn "+"
>>
>> -- explode [[[1,2],[3],[4,5,6]], [[1, 2], [14,15], [16]]] -->
>> [[1,3,4],[1,3,5],
>> -- [1,3,6],[2,3,4],[2,3,5],[2,3,6],[1,14,16],[1,15,16],[2,14,16], 
>> [2,15,16]]
>> explode :: [[[a]]] -> [[a]]
>> explode =  concatMap cp
>>
>> -- toSingles "8+12,11,7+13,10\n1+2+3,1+9,3+6,4" -->
>> --
>> [[8,11,7,10],[8,11,13,10],[12,11,7,10],[12,11,13,10],[1,1,3,4], 
>> [1,1,6,4],
>> --  [1,9,3,4],[1,9,6,4],[2,1,3,4],[2,1,6,4],[2,9,3,4],[2,9,6,4], 
>> [3,1,3,4],
>> --  [3,1,6,4],[3,9,3,4],[3,9,6,4]]
>> toSingles :: String -> [[Int]]
>> toSingles = explode . fromCSV . lines
>>
>> -- toCSV [8,11,7,10,12] --> "8,11,7,10,12"
>> toCSV :: (Show a) => [a] -> String
>> toCSV = tail . init . show
>>
>> main = do
>>  getContents >>= externalSort . toSingles >>= mapM_ (putStrLn .  
>> toCSV)
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 19, Issue 3
****************************************

Reply via email to