computer programming question (Please help!)

somethingwitty

Golden Member
Aug 1, 2000
1,420
1
0
(apologies in advance if this is in the wrong thread, but the assignment is due really soon, and OT tends to respond quicker)

I'm learning ML for my computer class, but, as the professor says, the languages dont really change that much. So here's a problem we have, and some of the solutions I've tried...

write a function that will accept a command and 2 lists of equal lengths (any type) and run the command on all combos of the list. example: (+) [1;2;3] [10;11;12] as input should output a list of [11;12;13;12;13;14;13;14;15]. get it? nine combos, all added together.

Obviously, this function is recursive-here's the ML version of what I've tried, along with the outputs I recieve-what am I missing?

key:
rec = means function will be recursive
tl = the tail of a list, i.e tl [1;2;3;4] will be [2;3;4]-everything but the head
hd = the head of the list, i.e hd [1;2;3;4] will be 1 (I believe that 1 is NOT a list anymore)
:: = concatinate into list form, must be same types


I try:
let rec mapPairs x y z= if (tl y)=[] then [] else x (hd y) (hd z)::if (tl z)=[] then mapPairs x (tl y) z else mapPairs x y (tl z);;
I get:
[11; 12; 13; 14]

I try:
let rec mapPairs x y z = if y = [] then [] else if (tl z)=[] then x (hd y) (hd z)::mapPairs x (tl y) z else x (hd y) (hd z) ::mapPairs x y (tl z);;
I get:
[11; 12; 13; 14; 15]

I try:
let rec mapPairs x y z = if y = [] then [] else x (hd y) (hd z)::if z = [] then (mapPairs x (tl y) z) else (mapPairs x (tl y) (tl z));;
I get:
[11; 13; 15]

And so on...that was just a random sampling of everything I've tried, but still nothing's working. If anyone here knows ML or can figure out the logic of the recursive function I'd appreciate the help.
 

arcain

Senior member
Oct 9, 1999
932
0
0
When doing recursion, you want to figure out the base case first. In this case it would probably be best to use an empty list as the base case. I don't know any ML so you'll have to work things out.

In psuedocode it would go something like this

function MyFunc( command , y , z)
{
if (y is empty and z is empty) return empty list;
else return command(head y, head z) concatonated with result of MyFunc(command, tail y, tail z);
}

And here is my attempt at translating into ML (bear in mind I've never seen ML code before):

let rec mapPairs x y z = if y = [] then [] else (x (hd y) (hd z)) :: mapPairs x (tl y) (tl z);;

It only checks one of the lists to be empty, so it would only work if the lists (y, z) are the same length (if the code works at all).
 

somethingwitty

Golden Member
Aug 1, 2000
1,420
1
0
arcain, thanks for the reply. unfortunately, I've already tried that code (and I tried it again now) and gotten the output of list [11; 13; 15] when inputting (+) [1;2;3] [10;11;12]. I'd assume that output comes from matching 1 with 10, then 2 with 11, then 3 with 12. I need to get it to try the inside combos too...any other suggestions?

Thanks
 

arcain

Senior member
Oct 9, 1999
932
0
0
Oops.. missed that part.

Ok then.. say given inputs x [y1 y2 y3] [z1 z2 z3], you want to loop through all of the y's processing them against the z list. The sets up your first base case:

1. if (y is empty) return empty list;

Now when y isn't empty, you just take the head of y and process through z, this is another loop. So somewhere in your code you'll have another condition:

2. if (z is empty) return empty list;

Let's try that now:
function MyFunc(command, y, z)
{
..if (y is empty) return empty list;
..else {
....if (z is empty) return empty list;
....else {
......return command(head(y), head(z)) :: MyFunc(command, head(y), tail(z)) :: MyFunc(command, tail(y), z);
....}
..}
}

Now converting that into ML looks a little uglier.. but it is kinda similar to your 2nd example.
 

bUnMaNGo

Senior member
Feb 9, 2000
964
0
0
ML? What's ML? Machine Language? :p I'm learning MAL and SAL right now... don't ask me what they stand for, but it's some neato ASM language that uses spimsal or xspimsal... pretty nifty... so far it has taken me 5 hrs to code a program that asks for a string, such as '5+5', and output something like '10' without the use of puti or geti :p
 

somethingwitty

Golden Member
Aug 1, 2000
1,420
1
0
yeah, I think ML is for machine language.

arcain, thanks for the efforts. The second one didnt work either, so i had to hand it in incomplete. Still, I'm curious how it should be done-I'll post when I find out, then I'll probably go off and kick myself.
 

arcain

Senior member
Oct 9, 1999
932
0
0
I just converted my 2nd algorithm into Scheme (no ML, and Scheme handles lists well). And the algorithm in general is correct save for a small bug.

function MyFunc(command, y, z)
{
..if (y is empty) return empty list;
..else {
....if (z is empty) return empty list;
....else {
......return command(head(y), head(z)) :: MyFunc(command, list(head(y)), tail(z)) :: MyFunc(command, tail(y), z);
....}
..}
}

The big return line gets changed a little, you need to convert the head(y) to a list(head(y)) (in other words convert the head of y into a list). I've tested a version in Scheme and it works fine.

If you know Scheme (or interested in the code) here it is:
(define (MAPPAIRS com y z)
....(cond
........((null? y) ())
........((null? z) ())
........(#t (append (list (com (car y) (car z))) (MAPPAIRS com (list (car y)) (cdr z)) (MAPPAIRS com (cdr y) z)))
....)
)

And the output:

1 ]=> (mappairs + '(1 2 3) '(10 11 12))

;Value 1: (11 12 13 12 13 14 13 14 15)
 

br0wn

Senior member
Jun 22, 2000
572
0
0
ML is not Machine Language.

It is Meta Language, one of the best and pure functional language
(considered better than lisp, scheme,...)
 

somethingwitty

Golden Member
Aug 1, 2000
1,420
1
0
we use ocaml, which stands for:
Cbjective Categorical Abstract Machine Language. So I'd guess ML would be machine language, but I'm actually not sure.