[Haskell-cafe] Software Tools in Haskell

Benja Fallenstein benja.fallenstein at gmail.com
Wed Dec 12 20:50:39 EST 2007


On Dec 13, 2007 2:28 AM, Benja Fallenstein <benja.fallenstein at gmail.com> wrote:
> Although on reflection, I think I might like the following compromise
> with Tillmann's version best:
>
> main = interact $ perLine $ detab 0 where
>     detab tab ('\t':cs) = replicate (4-tab) ' ' ++ detab 0                 cs
>     detab tab (char:cs) = char                  :  detab ((tab+1) `mod` 4) cs
>     detab tab ""        = ""

On more reflection, I wonder whether it would be worthwhile to have a
library function for folds that work from both left *and* right:

foldlr :: (a -> b -> c -> (a,c)) -> a -> c -> [b] -> (a,c)
foldlr f l r []     = (l,r)
foldlr f l r (x:xs) = let (l',r') = f l x r''; (l'',r'') = foldlr f l' r xs
                       in (l'',r')

main = interact $ perLine $ snd . foldlr detab 0 "" where
    detab tab '\t' cs = (0, replicate (4-tab) ' ' ++ cs)
    detab tab char cs = ((tab+1) `mod` 4, char : cs)

It's a fun function, anyway :-)

- Benja


More information about the Haskell-Cafe mailing list