[Haskell-beginners] Re: multreplace

jean verdier verdier.jean at gmail.com
Fri Jul 2 03:21:00 EDT 2010


I think your first attempt using list comprehension is nearly ok but is
in fact a fold:

multRepl str ss rs = foldl (uncurry . replace) str (zip ss rs)

or something similar.

On Thu, 2010-07-01 at 23:58 -0700, prad wrote:
> On Thu, 1 Jul 2010 22:31:28 -0700
> prad <prad at towardsfreedom.com> wrote:
> 
> > so back to
> > the drawing board!
> >
> here's what emerged:
> 
> ======
> #!/usr/bin/env runghc
> 
> module Main where
> 
> import Useful as U
> 
> main = do
> 
>     let str = "This is original string"
>     let ss  = ["orig","ing"]
>     let rs  = ["very orig","ucture"]
> 
>     putStrLn $ head (multRepl str ss rs)
>     
>     
> --multRepl :: String -> [String] -> [String] -> [String]
> multRepl [] _ _             = []
> multRepl str (s:ss) (r:rs)  = do
>     let newStr = U.replace str s r
>     if (length ss) == 0 
>        then return newStr 
>        else multRepl newStr ss rs 
> =======
> 
> this does produce the correct output:
> This is very original structure
> 
> 
> and here are my questions:
> 
> 1. the type
> *Main Useful> :t multRepl 
> multRepl :: (Eq t) => [t] -> [[t]] -> [[t]] -> [[t]]
> 
> but i have it returning newStr which equals U.replace str s r
> and the type of U.replace is String as shown below
> *Main Useful> :t Useful.replace
> Useful.replace :: (Eq a) => [a] -> [a] -> [a] -> [a]
> 
> so why is it returning [String] when newStr isn't a list of strings?
> 
> 2. is the way i've done it proper haskellian? it took me quite some
> time to think this out trying to find my way through the fog of
> imperative programming.
> 
> (my apologies for replying to my own posts - as well as my
> appreciation for your assistance)
> 




More information about the Beginners mailing list