I have this:
val a = ((1,2),(1,2,1),(1,2,3),5,(4,5,6));
I need a function that returns few "x" is in this tuple...
Example: Few "5" is in the tuple:
funMagic (a,5);
Must return 2
Help!
I have this:
val a = ((1,2),(1,2,1),(1,2,3),5,(4,5,6));
I need a function that returns few "x" is in this tuple...
Example: Few "5" is in the tuple:
funMagic (a,5);
Must return 2
Help!
There may be some mistake in your question. In SML (as well as other major statically typed functional programming languages such as OCaml and Haskell), tuples have different types depending on the number (and types) of the elements. For example, your tuple a
has the following type:
(int * int) * (int * int * int) * (int * int * int) * int * (int * int * int)
As a result, your function funMagic
can only take a quintuple (a tuple with 5 elements) whose elements are themselves tuples with 2, 3, 3, 1, and 3 elements, respectively (in fact, there is no "tuple with 1 element" in SML; assume that it is equivalent to the element itself), at least unless you adopt some type encoding trick. Do you perhaps mean lists (such as [[1,2],[1,2,1],[1,2,3],[5],[4,5,6]]
) rather than tuples?
The function would look similar to this one. I suggest, your main problem was not to create the function, but to access a position in a tuple of tuples.
funmag: (int*int)*int -> int
funMag (p:(int*int)*int) = #2(#1 p)
This function will "look up" the first position of the big tuple and then jump to the 2nd position of the small tuple. So for example our tuple is ((5,7),3) then our function will return 7