[Haskell-beginners] Re: multreplace

Patrick LeBoutillier patrick.leboutillier at gmail.com
Fri Jul 2 09:16:24 EDT 2010


Hi,

> --multRepl :: String -> [String] -> [String] -> [String]

Personally I woud use a list of pairs to represent the target/replacements:

multRepl :: String -> [(String,String)] -> String

That way you are guaranteed that for each target there is a
replacement and don't need to handle the cases where both lists have
different lengths.

multRepl str srs = foldl (\acc (s,r) -> replace acc s r) str srs

or in point-free form:

multRepl = foldl (\acc (s,r) -> replace acc s r)

or more succinctly (as mentionned before):

multRepl = foldl (uncurry . replace)


Patrick





> 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)
>
> --
> In friendship,
> prad
>
>                                      ... with you on your journey
> Towards Freedom
> http://www.towardsfreedom.com (website)
> Information, Inspiration, Imagination - truly a site for soaring I's
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


More information about the Beginners mailing list