1

I was interested in if there is a possible way to get length of tuple in sml?! See example

val tes = ((1,"test"),("test","some")) 
Lenght(tes) = 2

I want it for a problem solve there is a problem which says to get students list which contains list for each student information but student information are different in two types some are like

(1,"test","nick") 

and some are like

("name","nick")

So it wants to return the first element of each list in student lists see below:

((1,"test","nick"),("test2","nick2"),(2,"test3","nick3"))

Return > (1,"test2",2)

Here more info M Molbdnilo @molbdnilo

Mobinkh
  • 117
  • 1
  • 7
  • 2
    You can't write a function that takes tuples of different "tuplicities", so no, it's not possible. (Except trivially; you can of course write `fun length (a,b) = 2`) What are you planning to use this information for? (See the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem).) – molbdnilo May 05 '21 at 14:31
  • @molbdnilo i have a problem to slove and it askes me to get list of students info and return the first element of them the list of student contains list in two types some of them are like (id,"name","nick") and some of them are like ("name","nick") – Mobinkh May 05 '21 at 15:35
  • But then you have the problem that the type `(int * string * string) list` isn't the same as the type `(string * string) list` -- so the function that you are trying to write will be ill-typed. – John Coleman May 05 '21 at 15:44
  • 1
    I suspect that you're supposed to define a new data type, and have a list of those. – molbdnilo May 05 '21 at 15:45
  • Ye thats the problem i already tried everything and it seems its not possible with my knowledge if you guys know anyway to do that ? that would be awsome – Mobinkh May 05 '21 at 15:46
  • new data types still wont be same as some data are int string string and some are string string – Mobinkh May 05 '21 at 15:50
  • 2
    As has been mentioned, you *can't* have such a list as you mention in ML. It is fundamentally impossible – all elements of a list must have the same type. You must have missed some crucial part of the problem description. – molbdnilo May 05 '21 at 17:21

1 Answers1

5

An example of what you're most likely expected to do; define some useful sum types.

First, let's invent two ways to identify a person:

datatype Person = JustName of string
                | NameAndNumber of string * int

datatype Identifier = Name of string
                    | Number of int

then you can get an Identifier for a Person:

fun identifier (JustName n) = Name n
  | identifier (NameAndNumber (_, i)) = Number i

Let's test with some people:

- val people = [JustName "A", NameAndNumber ("B", 23), JustName "C", NameAndNumber ("D", 22)];
val people =
  [JustName "A",NameAndNumber ("B",23),JustName "C",NameAndNumber ("D",22)]
  : Person list

- map identifier people;
val it = [Name "A",Number 23,Name "C",Number 22] : Identifier list
molbdnilo
  • 64,751
  • 3
  • 43
  • 82