Andrea Rossato <[EMAIL PROTECTED]> writes:

> On Mon, Sep 18, 2006 at 04:16:55AM -0700, Carajillu wrote:
> > 
> > Wow! I'm starting to love this languaje, and the people who uses it!:)
> > 
> 
> You spoke too early. My code had a bug, a huge one...
> 
> this is the right one:
> 
> -- Replaces a wildcard in a list with the list given as the third argument
> substitute :: Eq a => a -> [a] -> [a] -> [a]
> substitute e l1 l2= [c | c <- check_elem l1]
>     where check_elem [] = l1
>           check_elem (x:xs) = if x == e then (l2 ++ xs) else [x] ++ 
> check_elem xs


I think it's nicer to do it like this:

   substitute e l l'
       = concat (map subst_elem l)
         where subst_elem x
                   | x == e = l'
                   | otherwise = [x]

since "subst_elem" has a more straightforward meaning than
"check_elem", and the concatenation is handled by a well
known standard function.

Also, it would usually be more useful to have the argument
to replace /with/ before the argument to replace /in/, so
that ("substitute '*' "wurble") is a function that replaces
all the '*'s in it's argument with "wurble"s.

And if you do that, you can write it like this:

   subst e l'
       = concat . map subst_elem
         where subst_elem x
                   | x == e = l'
                   | otherwise = [x]

-- 
Jón Fairbairn                                 [EMAIL PROTECTED]

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to