If they do I need to print them out and if they don't I need to discard them
The first list should be taken as input and the second list could be defined
as words = ["item1", "item2"]
Please could someone help me write a fuction which does this?
Thanks a lot
lists are not a datastructure that should be used here...
...but as this seems to be your homework the professor gave you...
...well try your solution by sour self. ;-)
Ciao,
Oliver
Sounds like you're using SML so I'll use OCaml just to confuse you.
> The first list should be taken as input and the second list could be defined
> as words = ["item1", "item2"]
>
> Please could someone help me write a fuction which does this?
First write a function that tests for membership in the list "words":
fun w -> List.mem w words
then use this to filter out words in the list that appear in the second
list:
List.filter (fun w -> List.mem w words) input
then iterate the print function over the resulting list to print the
input words that appear in the list "words":
List.iter print_endline
(List.filter (fun w -> List.mem w words) input)
Cheers,
Jon.
Would it be possible for you to give me some example haskell functions
please?
Thanks
"Dr Jon D Harrop" <j...@ffconsultancy.com> wrote in message
news:1145205626.8...@g10g2000cwb.googlegroups.com...
isn't it you who normally uses (and recommends) Maps and such stuff? ;-)
Why not converting list to Maps and using them then? ;-)
only a thought I had .... because you use OCaml....
Ciao,
Oliver
You can use a set like this:
include Set.Make(String)
let set_of_list list = List.fold_right add list empty in
iter print_endline (inter (set_of_list words) (set_of_list input))
As Oliver says, computing set-theoretic operations (like intersection) will
be much faster for large sets with OCaml's sets than with lists.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/ocaml_for_scientists/chapter1.html
compareLists :: Eq a => [a] -> [a] -> [a]
compareLists as bs = [ a | a <- as, (elem a bs)]
compareLists :: Eq a => [a] -> [a] -> [a]
compareLists as bs = [ a | a <- as, (elem a bs)]
Prelude> compareLists [0,3..60] [2,4..60]
[6,12,18,24,30,36,42,48,54,60]
did this run on ghci just to show that what Ben's LC worked as he had
advertised...
gene