【问题标题】:How would you convert this If, Then Else recursions to pattern matching?您将如何将此 If, Then Else 递归转换为模式匹配?
【发布时间】:2017-02-12 21:13:59
【问题描述】:

在 ML 中工作我有一个递归函数,用 if、then、else 风格编写。该函数有两种基本情况,Null List 和 Null tail of list。第一个基本情况很容易转换为模式匹配样式。

if(Null L) then false

变成

fun oddNum(nil) = false 

我不确定如何以模式匹配样式编写其他基本情况。

else if (Null tl(L)) then true

我试过了

| oddNum(nil(tl(L)) = true

我的问题是,在我编写第二个基本案例“|oddNum(nil(tl(L)) = true”之前,我不知道如何将列表 L 声明为 x::xs。

【问题讨论】:

    标签: pattern-matching ml


    【解决方案1】:
    fun oddNum nil = false
      | oddNum (_::nil) = true
      | oddNum (_::_::xs) = oddNum xs
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2021-01-09
      • 2023-01-13
      • 1970-01-01
      相关资源
      最近更新 更多