【问题标题】:SML - changing the value of multiple tuples in the listSML - 改变列表中多个元组的值
【发布时间】:2017-02-03 19:17:14
【问题描述】:

我是 SML 的新手。我正在编写一个函数,它接受 2 个 int (x,y) 和一个元组列表(命名框)作为输入。我的列表的长度可以改变。我想在列表中找到 2 个元素取决于 x 和 y 的元组。例如,我想检查是否有像 box1=(x,y) 这样的元组和像 box2=(x-2,y-3) 这样的另一个元组,如果它们都在列表中可用,那么它们的值应该改变并同时返回。我知道如何找到一个元组并使用 List.map 更改它的值。但是更新多个元组怎么样?

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)

【问题讨论】:

    标签: sml


    【解决方案1】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 2018-03-19
      • 2020-08-16
      相关资源
      最近更新 更多