Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function to compare two lists?

0 views
Skip to first unread message

robinwilson16

unread,
Apr 16, 2006, 8:58:39 AM4/16/06
to
I have two lists of words and need to compare them both to see if words in
the first list exist in the second list

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


Oliver Bandel

unread,
Apr 16, 2006, 12:20:26 PM4/16/06
to
robinwilson16 wrote:

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

Dr Jon D Harrop

unread,
Apr 16, 2006, 12:40:26 PM4/16/06
to
robinwilson16 wrote:
> I have two lists of words and need to compare them both to see if words in
> the first list exist in the second list
>
> If they do I need to print them out and if they don't I need to discard them

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.

robinwilson16

unread,
Apr 16, 2006, 1:21:37 PM4/16/06
to
Sorry, the language I am using is Haskell

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...

Oliver Bandel

unread,
Apr 16, 2006, 7:04:41 PM4/16/06
to
Hey Jon,

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

Jon Harrop

unread,
Apr 19, 2006, 2:42:48 PM4/19/06
to
Oliver Bandel wrote:
> 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....

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

Ben

unread,
May 7, 2006, 6:43:34 AM5/7/06
to
Ah, an easy one for Haskell:

compareLists :: Eq a => [a] -> [a] -> [a]
compareLists as bs = [ a | a <- as, (elem a bs)]

Ben

unread,
May 7, 2006, 6:44:01 AM5/7/06
to
Ah, an easy one for Haskell:

compareLists :: Eq a => [a] -> [a] -> [a]
compareLists as bs = [ a | a <- as, (elem a bs)]

Message has been deleted

genea

unread,
May 24, 2006, 5:59:54 PM5/24/06
to
List comprehensions as a programming tool are just too too wonderful
for problems of this nature... Just nothing like it!!

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

0 new messages