0

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.

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61

1 Answers1

2

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
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • I want to create a map kind of data structure using list having specification :(string * int) list where first element is key and second element is value. Initially when I create this map, I just want to give key with no value. How can I pass null value in this case? For example, I need something like this when initializing: val gamma = [("a" , _ ),("b" , _ ),("c" , _ )] :(string*int) list. – Ankit Shubham Apr 09 '16 at 11:22
  • I tried putting "_" and "NONE" but they didn't work. – Ankit Shubham Apr 09 '16 at 11:23
  • @AnkitShubham that sounds like a totally separate question. I suggest you ask a new one using the StackOverflow system. – Ionuț G. Stan Apr 09 '16 at 11:34