elementsinlist

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

elementsinlist

Jenny678
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

Reply | Threaded
Open this post in threaded view
|

Re: elementsinlist

Bryan Burgers
On 6/13/06, Jenny678 <[hidden email]> 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

I haven't looked too far into this, but how about something like this:

elements_in_list a b = and $ map (\x -> element_in_list x b) a
   where element_in_list = ...

Maybe if you don't want to use built-in functions, define your own
'and' as well.

Bryan
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: elementsinlist

Sebastian Sylvan-2
In reply to this post by Jenny678
On 6/14/06, Jenny678 <[hidden email]> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe