As quoify says, it's spelled valOf
.
And as kopecs says, if you use pattern matching, it will be much shorter:
fun max1 (x::y::rest) = max1 (Int.max (x, y) :: rest)
| max1 [x] = SOME x
| max1 [] = NONE
(This version also uses the library function Int.max
for added brevity.)
If this is too compact, you could also write:
fun max1 (x::y::rest) = let val z = Int.max (x, y) in max1 (z::rest) end
| max1 [x] = SOME x
| max1 [] = NONE
The version from the slides deals with an annoying situation that arises in many recursive functions that return sum types like 'a option
: You may need to perform a call, do some unpacking (i.e. remove SOME
), and then pack the result back (i.e. add SOME
again).
But the max1
problem does not necessitate that situation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…