Ankit Shubham
Ankit Shubham

Reputation: 3619

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.

Upvotes: 0

Views: 447

Answers (1)

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179179

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

Upvotes: 2

Related Questions