[Haskell-cafe] elementsinlist
Sebastian Sylvan
sylvan at student.chalmers.se
Tue Jun 13 19:34:43 EDT 2006
On 6/14/06, Jenny678 <mestor1 at gmx.de> wrote:
>
> Hallo
> I search a code for
>
> >elements_in_List([1,2],[1,2]).
> >True
> >elements_in_List([1,8],[1,2,3,4,8]).
> >True
> >elements_in_List([2,1],[1,2]).
> >True
> >elements_in_List([1,1],[1]).
> >False
>
> I have a code
> elements_in_List :: Eq a => [a] -> [a] -> Bool
> elements_in_List [] _ = True
> elements_in_List _ [] = False
> elements_in_List (x:xs) (y:ys)
> | x == y = elements_in_List xs ys
> | True = elements_in_List (x:xs) ys
>
> but it failed at
> >elements_in_List([2,1],[1,2]).
> >True
>
> I hope somebody can help me
> Please don't use built-in-Functions.
>
> Thanks for Help
>
This sounds like homework so I'll just give some hints.
I think you would benefit from splitting this problem up a bit into a
couple of functions. There are many ways to solve it but think about
what kind of functions would help you solve this problem.
What if you had these two functions
isInList :: Eq a => a -> [a] -> Bool
removeFromList :: Eq a => a -> [a] -> [a]
Which would test for membership and remove a single element from a
list respectively. Would that help you?
Could you write these two functions yourself?
You could combine them of course:
findAndRemove :: Eq a => a -> [a] -> (Bool,[a])
or maybe even
findAndRemove :: Eq a => a -> [a] -> Maybe [a]
In the second case Nothing would represent that the element wasn't
found. This way would be better from an efficiency standpoint, but it
might be easier to define the two functions mentioned above (isInList
and removeFromList).
Try to figure out how to use these functions to achieve what you want,
then write them (and test independently!).
Regards,
--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
More information about the Haskell-Cafe
mailing list