[Haskell-cafe] Re: Problems interpreting

Bulat Ziganshin bulat.ziganshin at gmail.com
Mon Sep 18 08:52:33 EDT 2006


Hello Andrea,

Monday, September 18, 2006, 4:23:21 PM, you wrote:

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

> Pretty. Just to many keystrokes.
> This should take two keystrokes less, probably:

> subst e l [] = []
> subst e l (x:xs) = if x == e then l ++ xs else x : subst e l xs

but the goal is not keystrokes itself but easy of understanding. for
me, first solution looks rather idiomatic and "intuitively"
understandable. second solution requires more time to "got it", but
seems easier for novices that are not yet captured higher-level
Haskell idioms. i just want to said that it will be easier to read it
if you split it into several lines:

subst e l [] = []
subst e l (x:xs) = if x == e
                     then l ++ xs
                     else x : subst e l xs

or

subst e l []                 = []
subst e l (x:xs) | x==e      = l ++ xs
                 | otherwise = x : subst e l xs

and that your solution substitutes only first match in a list:

subst 1 [1,1] [0] = [0,1]


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list