[Haskell-cafe] IterIO: How to write use my inumReverse

dm-list-haskell-cafe at scs.stanford.edu dm-list-haskell-cafe at scs.stanford.edu
Mon Jul 4 15:54:38 CEST 2011


At Mon, 4 Jul 2011 20:36:33 +1000,
John Ky wrote:
> 
> Hi Haskell Cafe,
> 
>           enum |$ inumLines .| inumReverse .| inumUnlines .| iter
> ...
>    
>     iterLines :: (Monad m) => Iter L.ByteString m [L.ByteString]
>     iterLines = do
>       line <- lineI
>       return [line]
>    
>     iterUnlines :: (Monad m) => Iter [L.ByteString] m L.ByteString
>     iterUnlines = (L.concat . (++ [C.pack "\n"])) `liftM` dataI
>    
>     iterReverse :: (Monad m) => Iter [L.ByteString] m [L.ByteString]
>     iterReverse = do
>       lines <- dataI
>       return (map L.reverse lines)
>    
>     inumLines = mkInum iterLines
>     inumUnlines = mkInum iterUnlines
>     inumReverse = mkInum iterReverse
> 
> It all works fine.
> 
> My question is: Is it possible to rewrite inumReverse to be this:
> 
>     iterReverse :: (Monad m) => Iter L.ByteString m L.ByteString
>     iterReverse = do
>       line <- dataI
>       return (L.reverse line)
>    
>     inumReverse = mkInum iterReverse
> 
> And still be able to use it in the line:
> 
>     enum |$ inumLines .| {-- inumReverse goes in here somehow --} .|
>     inumUnlines .| iter
> 
> The reason I ask is that the Haskell function reverse has the type [a] -> [a],
> not  [[a]] -> [[a]].
> 
> I thought perhaps the alternative inumReverse is cleaner than the original as
> it behaves more similarly to Haskell's own reverse function.

I'm not sure what you are trying to achieve.  If you want an iter that
works on L.ByteStrings, then you can say:

     iterReverse :: (Monad m) => Iter L.ByteString m L.ByteString
     iterReverse = do
       line <- lineI
       return (L.reverse line)
    
In that case you don't need inumLines and inumUnlines.  If, however,
you want the type to be [L.ByteString], and you would rather do this
one line at a time, instead of calling map, then you could do
something like the following:

     iterReverse :: (Monad m) => Iter [L.ByteString] m [L.ByteString]
     iterReverse = do
       line <- headI
       return [L.reverse line]

But the code you have above should also work, so it all depends on
what you are trying to achieve.

David



More information about the Haskell-Cafe mailing list