简体   繁体   中英

Editing tuples in a list of sml

I want to make a list which is of specification :(string*int) list and the tuples can be edited. For example, suppose

val gamma = [("a",20),("b",30),("c",40)] :(string*int) list

Now, how can I change the value 30 in the tuple ("b",30) to , let's say, 70.

You need to map over the list and build a new tuple:

let
  fun change key value (k, v) =
    if k = key
    then (k, value)
    else (k, v)

  val list = [("a",20),("b",30),("c",40)]
in
  List.map (change "b" 70) list
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM