|
Author: P--K
Date: 02-03-04 16:16
Hi Guys,
I am really stuck with some functions i wanna do in Caml can anybody help?
Define a function CountNi which takes a list of integers as its argument and returns the number of negative integers in the list.
And also...
Define a function CommonElement which takes two lists as its arguments
and then returns the list consisting of all common elements of them. For
example, when the two input lists are [1;2;3;4;5;6;7] and [1;3;5;7;9;11], the
result should be [1;3;5;7].
Cheers guys for any help
|
|
Reply To This Message
|
|
Author: danp
Date: 03-03-04 21:53
Sounds like a homework problem ... If you have specific questions, then you should ask those (maybe on a more appropriate forum ;). I'm not sure what help you are expecting here.
Here's how I'd think about the CountNi problem: As the problem states, you want a function that takes a list and returns a number. What can you do with a list? If you look at the example functions that you must surely have, you'll see that you can look at a list as a head and a tail. The head is an integer. It's easy to tell if an integer is negative or not. The tail is list. What can be done with that? [slams fist into hand] If only we had a function that counted the negative integers in a list -- oh, wait, CountNi does that. Sounds kind of recursive don't you think? ;) The preceding case handles everything except where there is no head, i.e. the empty list. It's easy to count the negative integers in an empty list.
The second problem sounds tricky ... It seems a little underspecified to me. Are you allowed to use library modules? I'm guessing that a particular module in the standard library might be useful.
|
|
Reply To This Message
|
|