【问题标题】:How can I delete an element in the record field?如何删除记录字段中的元素?
【发布时间】:2015-01-04 23:30:42
【问题描述】:

我是 SML 的新手,我正在从可以在 google 和 stackoverflow 上获得的材料中学习。

为了好玩,我只是尝试做一些随机的事情,例如:

type schedule= { transportation:string, go: string list}

val sunday:schedule ={ transportation="Bicycle", go=["gym","walmart","dentist"]}

我想在访问过该地点后从我的记录中删除。

fun del("walmart", sunday);=> { transportation="Bicycle", go=["gym","dentist"]}

从这个link 我知道如何从普通列表中删除元素。 我的问题是我不知道如何访问记录中的列表并删除。

【问题讨论】:

    标签: list types record sml ml


    【解决方案1】:

    您可以使用模式匹配从记录中提取值。您可以执行您需要做的更改,并构建包含更新值的新记录。

    例如,如果我有个人记录:

    val john = { name = "John", age = 23, country = "Denmark" }
    

    如果我想增加他们的年龄,我可以这样做:

    fun updateAge {name, age, country} = { name = name, age = age + 1, country = country }
    val john' = updateAge john
    

    所以,简而言之:提取字段,以您知道的方式从列表中删除元素,然后重建记录。

    【讨论】:

    • ,所以我可以用 (#go schedule) 提取它并将它传递给另一个名为 delete 的函数,对吧?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多