【问题标题】:Can I fold with an infix operator without writing out an anonymous function?我可以用中缀运算符折叠而不写出匿名函数吗?
【发布时间】:2013-02-15 07:42:00
【问题描述】:

如果我想添加一个列表,我可以这样做:

- List.foldr (fn (x, y) => x + y) 0 [1, 2, 3]
val it = 6 : int

有没有办法写出更多类似的东西:

List.foldr + 0 [1, 2, 3]

我尝试过这样的事情:

fun inf2f op = fn (x, y) => x op y;

【问题讨论】:

    标签: sml syntactic-sugar


    【解决方案1】:

    你已经接近了。在第二个示例中添加op 关键字。

    - List.foldr op + 0 [1,2,3];
    val it = 6 : int
    

    【讨论】:

    • 后续,有没有像(?? f)这样代表(fn x => f x)的?
    • 呃……这不就是f吗?
    • @brian,只是为了正确起见。 op 是关键字,就像 ifwithopen 一样。不是操作员。
    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 2023-03-25
    • 2019-05-15
    • 2021-10-11
    • 2011-05-05
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多