[Haskell-cafe] Problems interpreting

Carajillu crespi.albert at gmail.com
Mon Sep 18 09:38:14 EDT 2006


Definitely I'll take this solution, I'm reading about Pointfree, I think it's
not that dificult to understand. And moreover it's the simpliest way to
write code.


Jón Fairbairn-2 wrote:
> 
> Andrea Rossato <mailing_list at istitutocolli.org> 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                                 Jon.Fairbairn at cl.cam.ac.uk
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

-- 
View this message in context: http://www.nabble.com/Problems-interpreting-tf2290155.html#a6363827
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list