Sml tuples length

210 views Asked by At

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

1

There are 1 answers

0
molbdnilo On BEST ANSWER

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