2017-03-08 119 views

回答

1

這是一個提示。使用相同的基本邏輯。編寫一個函數minmax以返回一對形式爲(min,max)的值,然後在遞歸步驟中,使用模式匹配來提取這兩個值。模板是:

fun minmax [] = raise Empty 
| minmax [x] = (x,x) 
| minmax (x::xs) = 
    let 
    val (a, b) = minmax xs 
    in 
    <fill in the code> 
    end; 

在上面a將是最小和b最大。根據ab和第三個值x,返回(x,b)(a,x)(a,b),具體取決於不等式如何發揮。您將需要不止一個if

1

這是一個不同的提示:使用一個輔助函數來存儲當前的(最小值,最大值),並在完成迭代時返回這些值。模板爲:

fun minmax [] = raise Empty 
    | minmax (y::ys) = 
    let fun helper [] (min, max) = ... 
      | helper (x::xs) (min, max) = ... 
    in helper ys (y, y) end 
相關問題