SML How to define proper option

前端 未结 1 730
再見小時候
再見小時候 2021-01-15 06:02

Why doesn\'t the following code doesn\'t work?

fun sum_list xs =
case xs of
    [] => NONE
 | x::xs\' => SOME (x+sum_list xs\')

This

相关标签:
1条回答
  • 2021-01-15 06:30

    The problem is that the function is made to return an 'a option but in the lat expression you use it as if it returned an int:

    x + sum_list xs'
    

    You have to either build an internal function that works solely in lists and return numbers (not options), and then box the final result into an option, or eval the result of sum_list when returned to see if it contains a value or it is NONE. I can write both ways, but you should try before seeing the solution written.

    0 讨论(0)
提交回复
热议问题