-1

I have seen this question for Python, But I have the same question for SML (PolyMl).

I want to create a function to extract from a list of tuples (int, string) the string value of the tuple with minimum int.

For example, if I have this list:

l = [('a', 5), ('b', 3), ('c', 1), ('d', 6)]

The output should be 'c', because the minimum integer is in the tuple ('c', 1). Thank You!

Makarius
  • 2,165
  • 18
  • 20
Nemo
  • 1
  • 6

2 Answers2

2
val l = [(#"a", 5), (#"b", 3), (#"c", 1), (#"d", 6)]

fun min [] = NONE
  | min [x] = SOME x
  | min ((c1,n1) :: (c2,n2) :: xs) = if n1 < n2 then
                                       min ((c1,n1) :: xs) 
                                     else
                                       min ((c2,n2) :: xs)

val SOME (c,_) = min l
gruenewa
  • 1,666
  • 11
  • 16
0
val lst = [(#"a", 5), (#"b", 3), (#"c", 1), (#"d", 6)];

(* The first item of a tuple is fetched with #1. *)
#1(List.foldl
    (fn ((new as (_,n)), (old as (_,n0))) =>
      if n < n0 then new else old)
    (hd lst)
    (tl lst));

(* val it = #"c" : char *)
to_the_crux
  • 257
  • 1
  • 6