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