algorithm - SML : Changing value of INT in a structure

标签 algorithm types sml smlnj

我有一个小问题。

我进行了计算,但我所有的 ID 都不再相互关联(因为在计算过程中进行了一些删除)。不幸的是,为了使我的结果有效,我需要这个顺序......:/

所以为了简化任务,我制作了一个外部函数,它将“重命名”所有 ID,但我不知道如何执行此操作。

这是我得到的功能:

fun setId (W {id, ...}) = 
let 
in
    print( "[" ^ Int.toString (id) ^ "]");
    print( "[" ^ Int.toString (!nextId) ^ "]\n");
    Ref.incr nextId
end

(对于徘徊的人来说,app只是一个自制函数,用于对列表的每个元素执行相同的计算,但这并不重要。)

当我执行这段代码时,我得到了输出:

[0][0]
[1][1]
[2][2]
[3][3]
[4][4]
[5][5]
[6][6]
[7][7]
[8][8]
[9][9]
[10][10]
[11][11]
[12][12]
[13][13]
[14][14]
[15][15]
[16][16]
[17][17]
[18][18]
[19][19]
[20][20]
[21][21]
[22][22]
[39][23]
[40][24]
[41][25]
[42][26]
[43][27]
[44][28]
[45][29]
[46][30]
[47][31]
[48][32]
[49][33]
[50][34]
[51][35]
[52][36]
[53][37]

如您所见,[23] [39] 存在一个大问题,该列表没有以下数字。 :/

基本上,我希望函数 setId 能够修改节点的 ID。但我不知道如何:/

这里是数据类型 Node 用于理解目的:

    datatype node =
          W of {
              id              : int
            , predId          : int option
            , creationDepcy   : Dependency.depcy
            , nominalDepcies  : Dependency.depcy list ref
            , pattern         : Termstore.store
            , propositions    : Propstore.pstore
            , nominals        : Propstore.pstore
            , diamonds        : Termstore.store
            , boxes           : Termstore.store
            , disjunctions    : Termstore.store
            , nglstore        : Termstore.store
            , lazyProps       : Lazystore.store
            , lazyNoms        : Lazynomstore.store
            , lazyBoxes       : Lazyboxstore.store
            , blockedDiamonds : (Term.index * int) list ref
            , branchPoints    : int list ref
            }

在此先感谢您的帮助!

最好的问候。

最佳答案

由于 idint 类型,因此无法修改。如果将其更改为 int ref,则可以修改它,但您还必须更改访问器以取消引用 ref

另一种解决方案是创建一个从旧 ID 映射到新 ID 的数组,并使用该数组进行展示,但这似乎更加复杂。

关于algorithm - SML : Changing value of INT in a structure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34637134/

相关文章:

c++ - 在 C++ 中获取类名有哪些好的技巧?

smlnj - 查找字符串是否是 SML NJ 中其他字符串的子字符串

algorithm - 中止 bool RPN 表达式的评估

c++ - 可变参数模板类中的定义数量不同

haskell - 关于嵌套 CPS 悬架类型

syntax-error - 有什么作用? SML中的数据类型是什么意思?

syntax - SML 的 EQUALOP 错误消息

python - 计算多变量函数在 Python 中的间隔之间取值的有效方法

algorithm - 在不使用固定长度变量存储结果的情况下将十六进制数转换为十进制形式的最快算法

string - 后缀树如何工作?