0

I am trying to create a function which interleaves a pair of triples such as ((6, 3, 2), ( 4, 5 ,1)) and create a 6-tuple out of this interleaving. I made some research but could understand how interleaving is supposed to work so I tried something on my own end ended up with a code that is creating a 6-tuple but not in the right interleaved way. This is my code

let interleave ((a, b, c), (a', b', c')) =
let sort2 (a, b) = if a > b then (a, b) else (b, a) in
let sort3 (a, b, c) = 
let (a, b) = sort2 (a, b) in
let (b, c) = sort2 (b, c) in
let (a, b) = sort2 (a, b) in
(a, b, c) in
let touch ((x), (y)) = 
let (x) = sort3 (x) in
let (y) = sort3 (y) in
((x),(y)) in
let ((a, b, c), (a', b', c')) = touch ((a, b, c), (a', b', c')) in
(a, b', a', b, c, c');;

Can someone please explain to me how with what functions I can achieve a proper form of interleaving. I haven't learned about recursions and lists in case you would ask why I am trying to do it this way. Thank you already.

Uysal M
  • 87
  • 6
  • I don't understand what you're trying to do. To me "interleave" means something like `let interleave ((a, b, c), (a', b', c')) = (a, a', b, b', c, c');;`; but it's clear from your code that you're trying to do much more than that. Can you explain exactly what behavior you want your function to have? – ruakh Oct 15 '16 at 00:45
  • To be honest I am lost. As I mentioned above I thought interleaving has some kind of a pattern. I guess I just misunderstood the given description. Which was something like this "a function that takes a pair of triples and forms a 6-tuple representing the maximum that can be created using the pair if triples". So I am poorly attempting to create the max out of the two triples. I hope that was clear I am very confused right now. Thank you – Uysal M Oct 15 '16 at 01:04

2 Answers2

2

The problem statement uses the word "max" without defining it. If you use the built-in compare function of OCaml as your definition, it uses lexicographic order. So you want the largest value (of the 6 values) in the first position in the 6-tuple, the second largest value next, and so on.

This should be pretty easy given your previously established skill with the sorting of tuples.

For what it's worth, there doesn't seem to be much value in preserving the identities of the two 3-tuples. Once inside the outermost function you can just work with the 6 values as a 6-tuple. Or so it would seem to me.

Update

From your example (should probably have given it at the beginning :-) it's pretty clear what you're being asked to do. You want to end up with a sequence in which the elements of the original tuples are in their original order, but they can be interleaved arbitrarily. This is often called a "shuffle" (or a merge). You have to find the shuffle that has the maximum value lexicographically.

If you reason this out, it amounts to taking whichever value is largest from the front of the two tuples and putting it next in the output.

This is much easier to do with lists.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • The example given in the description states that (6, 4, 5, 3, 2, 1) "645321" is the max number that can be obtained from (6, 3, 2) and (4, 5, 1). Therefore putting the largest value in the 1st position and following the order isn't going to work. From the above I guess I should be comparing the 1st and 2nd digits of the latter tuple to the 1st element in the first then sort them in that order. I guess I can make that work with if statements. – Uysal M Oct 15 '16 at 01:19
  • Aha, this is because of the "interleaving". I get it. I'll update my answer. – Jeffrey Scofield Oct 15 '16 at 01:26
  • Thank you, that update made me understand it for sure. Am I in the right direction `let interleave ((a, b, c), (a', b', c')) = if a <= a' then (a', a, b, c, b', c') else (a, a', b, b', c, c') in if a <= b' then (a', b', a, b, c, c') else (a', a, b, c, b', c') in if a <= c' then (a', b', c', a, b, c) else (a', b', a, b, c, c')` or swimming in the wrong seas ? :-D – Uysal M Oct 15 '16 at 01:56
  • I definitely understood the idea but to put it into code is a bit hard right now. I have been on it for too much know and can't focus. Thank you for all your answers and comments. I actually learned a lot. – Uysal M Oct 15 '16 at 01:58
  • Please ignore the comment with if statements makes no sense :-D – Uysal M Oct 15 '16 at 02:02
2

Now that I understand what your end-goal is . . .

Since tuples of n elements are different types for different n's, you need to define helper functions for manipulating different sizes of tuples.

One approach, that basically mimics a recursive function over lists (but requires many extra functions because of tuples all having different types), is to have two sets of helper functions:

  • functions that prepend a value to an existing tuple: prepend_to_2, up through prepend_to_5. For example,

    let prepend_to_3 (a, (b, c, d)) = (a, b, c, d)
    
  • functions that interleave two tuples of each possible size up to 3: interleave_1_1, interleave_1_2, interleave_1_3, interleave_2_2, interleave_2_3, and interleave_3_3. (Note that we don't need e.g. interleave_2_1, because we can just call interleave_1_2 with the arguments in the opposite order.) For example,

    let interleave_2_2 ((a, b), (a', b')) =
        if a > a'
        then prepend_to_3 (a, interleave_1_2 (b, (a', b')))
        else prepend_to_3 (a', interleave_1_2 (b', (a, b)))
    

    (Do you see how that works?)

Then interleave is just interleave_3_3.

With lists and recursion this would be much simpler, since a single function can operate on lists of any length, so you don't need multiple different copies of the same logic.

ruakh
  • 175,680
  • 26
  • 273
  • 307