相关疑难解决方法(0)

从 SML 中的列表中获取最大值

我目前正在学习 SML,我很难理解下面的代码

fun good_max (xs : int list) =
  if null xs
  then 0
  else if null (tl xs)
  then hd xs
  else
    (* for style, could also use a let-binding for (hd xs) *)
    let val tl_ans = good_max(tl xs)
    in
      if hd xs > tl_ans
      then hd xs
      else tl_ans
    end
Run Code Online (Sandbox Code Playgroud)

hd xs是类型,int并且tl_ans,我认为是类型list。为什么这段代码有效?系统如何评估递归?xs = [3, 4, 5]如果您能向我展示这是如何工作的,那就太好了。

recursion list sml

3
推荐指数
1
解决办法
3020
查看次数

标签 统计

list ×1

recursion ×1

sml ×1