问题描述:

I'm new to SML. I'm writing a function which accepts 2 int (x,y) and a list of tuples (named boxes) as input. The length of my list can change. I want to find 2 tuples in the list whose elements depends on x and y. for example I want to check whether there is a tuple like box1=(x,y) and another tuple like box2=(x-2,y-3) and if both of them are available in the list then their values should be changed and returned simultaneously. I know how to find a tuple and change it's value using List.map . but how about updating multiple tuples?

fun move(x,y,boxes:(int * int)list) =

if List.exists (fn s => s = (x,y)) boxes andalso

List.exists (fn p => p = (x-1,y-2)) boxes

then ... (then for example how to change their value to box1=(x-1,y-2)

and box2=(x-3,y-4) at the same time and update them in the list)

网友答案:
List.map (fn p =>
  if p = (x, y) then (x-1, y-2)
  else if p = (x-2, y-3) then (x-3, y-4)
  else p
) boxes
相关阅读:
Top