Perl Christmas Quiz

Hakim Cassimally hakim.cassimally at gmail.com
Mon Dec 15 19:32:57 GMT 2008


On 15/12/2008, Robin Barker <Robin.Barker at npl.co.uk> wrote:
>  My haskell solution:
>
>  import List (delete)
>  intersection :: Eq a => [a] -> [a] -> [a]
>  intersection [] _ = []
>  intersection (x:xs) ys = if elem x ys then x: (intersection xs (delete x ys))
>                                                         else intersection xs ys

Nice!  And instead of explicit recursion we can tweak to a fold with
an accumulator that contains a pair containing the ys still left and
the result so far:

intersection xs ys = snd $ foldr aux (ys, []) xs
        where aux x acc@(ys, r) = if x `elem` ys then (delete x ys,
x:r) else acc

osf'


More information about the london.pm mailing list