Computer Science Canada

School Project Using Miranda

Author:  rar [ Mon Sep 21, 2009 7:58 pm ]
Post subject:  School Project Using Miranda

I don't think I've posted much here before except for in the Off-Topic forum. I signed up awhile ago in order to prepare in case I needed help.

Alright I'm in a first year class learning to use Miranda. There are reasons for starting with Miranda but I won't get into that.

I have an assignment and I've done all but this one program, and I was hoping someone could help me out...here is the question:

"(Make) A program which takes two lists of numbers and returns a list of numbers which are common to both input lists. HINT use the list operators (++ and/or --)"

Any help with this would be MUCH appreciated!!!

Author:  DemonWasp [ Mon Sep 21, 2009 8:27 pm ]
Post subject:  RE:School Project Using Miranda

Well, first you should probably read the Wikipedia article on Miranda. It may also be helpful to find some more in-depth documentation, though that's beyond my helpfulness.

Next, you should probably figure out how you would solve the problem on paper - write down a bunch of numbers in two columns, then find all the numbers that occur in both lists yourself.

From your own pen-and-paper algorithm, determine what the computer needs to do to mimic you. This will require, at the very least, your two base lists, plus the list you use as the result.

If you run into specific difficulties, return here with a more specific request for help. Posts with error messages; in particular "I tried this using this code, and got thisresult" is a great way to get detailed, useful help.

Author:  OneOffDriveByPoster [ Mon Sep 21, 2009 8:49 pm ]
Post subject:  Re: School Project Using Miranda

Are you allowed to use the built in definitions from the "standard environment"?

Author:  rar [ Mon Sep 21, 2009 8:49 pm ]
Post subject:  Re: School Project Using Miranda

OneOffDriveByPoster @ Mon Sep 21, 2009 8:49 pm wrote:
Are you allowed to use the built in definitions from the "standard environment"?


Do you mean things like 'foldr'?

Author:  OneOffDriveByPoster [ Mon Sep 21, 2009 8:50 pm ]
Post subject:  Re: School Project Using Miranda

rar @ Mon Sep 21, 2009 8:49 pm wrote:
Do you mean things like 'foldr'?
yes

Author:  rar [ Mon Sep 21, 2009 8:52 pm ]
Post subject:  RE:School Project Using Miranda

yes definitely, for one of the other questions it was even required.

Author:  OneOffDriveByPoster [ Mon Sep 21, 2009 9:06 pm ]
Post subject:  Re: School Project Using Miranda

I suppose from your description, you need to produce a function which takes two lists of numbers and returns one.
The first step is to figure out what to do if one or both of the lists are empty. What is common between the two lists then?

Author:  rar [ Mon Sep 21, 2009 9:13 pm ]
Post subject:  RE:School Project Using Miranda

Alright, but how do you figure out which numbers are common between the 2 lists? Some advice I received from someone in my class:
"what i did was
sorted both lists, then i removed the duplicate elements in each list
so that means each list only consisted of unique elements
and then
you check"

Does this have any merit?

Author:  OneOffDriveByPoster [ Mon Sep 21, 2009 9:19 pm ]
Post subject:  Re: RE:School Project Using Miranda

rar @ Mon Sep 21, 2009 9:13 pm wrote:
Alright, but how do you figure out which numbers are common between the 2 lists? Some advice I received from someone in my class:
"what i did was
sorted both lists, then i removed the duplicate elements in each list
so that means each list only consisted of unique elements
and then
you check"

Does this have any merit?
You can use sorting, but you do not have to.

Author:  rar [ Mon Sep 21, 2009 9:20 pm ]
Post subject:  RE:School Project Using Miranda

Alright, is it easier not to?

Author:  OneOffDriveByPoster [ Mon Sep 21, 2009 9:21 pm ]
Post subject:  Re: School Project Using Miranda

OneOffDriveByPoster @ Mon Sep 21, 2009 9:06 pm wrote:
The first step is to figure out what to do if one or both of the lists are empty.
Hopefully, you have a solution that works when at least one list is empty.

Now what if you have a one-element list and another non-empty list?

A two-element list and another non-empty list? Can you use the one-element solution to help solve the two-element case?

Can you use (no matter how trivially) the empty-lists solution to help solve the one-element case?

Can you generalize? For example, in the one-element case, the element you have in the one-element list to work with is also the first element.
To reach the one-element case from the two-element case, you did something with one of the two elements. What if you chose to do something with the first element?

In Miranda, it seems the "--" operation is more powerful than usual.

Author:  rar [ Mon Sep 21, 2009 9:23 pm ]
Post subject:  RE:School Project Using Miranda

I'm assuming generalizing would be easiest anyway, but yes I believe that's the intent of the program.

Author:  OneOffDriveByPoster [ Mon Sep 21, 2009 9:24 pm ]
Post subject:  Re: RE:School Project Using Miranda

rar @ Mon Sep 21, 2009 9:20 pm wrote:
Alright, is it easier not to?
What does "--" do with [7,7] -- [7]?

Author:  rar [ Mon Sep 21, 2009 9:25 pm ]
Post subject:  Re: RE:School Project Using Miranda

OneOffDriveByPoster @ Mon Sep 21, 2009 9:24 pm wrote:
rar @ Mon Sep 21, 2009 9:20 pm wrote:
Alright, is it easier not to?
What does "--" do with [7,7] -- [7]?


[7]?

Author:  DtY [ Mon Sep 21, 2009 9:28 pm ]
Post subject:  RE:School Project Using Miranda

If nothing else works, loop (or recurse?) over each item in the first list, and check if it's in the second.

Author:  OneOffDriveByPoster [ Mon Sep 21, 2009 9:34 pm ]
Post subject:  Re: RE:School Project Using Miranda

rar @ Mon Sep 21, 2009 9:25 pm wrote:
OneOffDriveByPoster @ Mon Sep 21, 2009 9:24 pm wrote:
What does "--" do with [7,7] -- [7]?
[7]?
I think using "--" is overkill if you are going to sort anyway. But sorting is a good way to do this. Consider:
code:
head :: [num] -> num
head (b:c) = b

tail :: [num] -> [num]
tail (b:c) = c

Author:  rar [ Tue Sep 22, 2009 10:50 am ]
Post subject:  RE:School Project Using Miranda

code:
program x y = x -- (x -- y)


-_- Feel like an idiot now Neutral It's so simple!

Thanks for the help!

Author:  Alexmula [ Tue Sep 22, 2009 5:05 pm ]
Post subject:  RE:School Project Using Miranda

some1 from uWindsor Very Happy


: