【发布时间】:2021-03-23 10:51:16
【问题描述】:
我之前已经发布过类似的问题,但我认为我需要重新表述我的问题。在我想要完成的事情上,我早些时候得到了一些很大的帮助。
所以,我现在想知道的是如何在我的代码中传递 comp 函数,以便它可以自定义。
我希望能够跑步
insertSorted(5, fn(a, b) => a > b, [8, 6, 3, 1];
哪个会返回
val it = [8, 6, 5, 3, 1]
同时还可以翻转标志并能够运行
insertSorted(5, fn(a, b) => a < b, [8, 6, 3, 1];
哪个会返回
val it = [1, 3, 5, 6, 8]
这是我目前所拥有的:
* insertSorted *
fun insertSorted (x, comp, nil) = [x]
| insertSorted (x, comp, y::ys) = if x comp y then y::x::ys else y :: insertSorted (x,ys);
这是有问题的“comp”: 第 2 行: if x comp y then y::x::ys else y :: insertSorted (x,ys);
【问题讨论】: