SV: Haskell-beginners problem with memory consuption

Ketil Malde ketil+haskell at ii.uib.no
Thu Oct 2 10:02:05 EDT 2003


Petter Egesund <petter.egesund at kunnskapsforlaget.no> writes:

> fun :: String -> String
> 	look for pat1 in string - if found subst with sub1
> 	look for pat2 in string - if found subst with sub2
> 	look for pat3 in string - if found subst with sub3
>   recurse until no pattern is found

I would structure this as 

        replace :: String -> [String] -> String
        replace input patterns = ...

At each position of input, check for the presence of
patterns (use isPrefixOf) and conditionally replace it, else output
first char of input and recurse on input's tail.

This should make the function "online", so that it can be streamed
over a large but lazy list (say a file read with "readFile" -- hurry
up before Simon takes it away! :-) without explicitly constructing the
whole input or output String.

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants


More information about the Haskell-Cafe mailing list