-1

我已经看到了 Python 的这个问题,但我对 SML(PolyMl)也有同样的问题。

我想创建一个函数来从元组列表中提取(int, string)具有 minimum 的元组的字符串值int

例如,如果我有这个列表:

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

输出应该是'c',因为最小整数在元组中('c', 1)。谢谢你!

4

2 回答 2

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
于 2015-02-18T20:50:50.763 回答
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 *)
于 2016-03-01T18:42:31.337 回答