【发布时间】:2020-07-09 14:33:09
【问题描述】:
我找到了华盛顿大学的旧课程 CSE341:编程语言,并尝试继续学习,以便有朝一日能够解决 Paulson 的 面向工作程序员的 ML。所以我有这个:
fun number_in_month5 (m : int, ms : (int*int*int) list) =
List.length (List.filter (fn (_,x2,_) => x2 = m) ms)
这给了我一个匹配的三元组列表。我可以在前面扔一个List.length 并得到计数。但是这个呢
fun number_in_month6 (m, (_,d2,_) :: ms) =
List.length (List.filter (fn (_,x2,_) => x2 = m) ms)
: stdIn:279.36 Warning: calling polyEqual
: stdIn:278.5-279.43 Warning: match nonexhaustive
: (m,(_,d2,_) :: ms) => ...
:
: val number_in_month6 = fn : ''a * ('b * ''a * 'c) list -> ('b * ''a * 'c) list
测试有时会给出错误的答案
- number_in_month6 (2,[(2018,2,1),(2018,2,2),(2018,1,3),(2018,2,4)])
val it = 2 : int
为什么这不起作用?但是我没有阅读 ML 错误消息的技能。有人建议不要在函数开始时声明类型——我尝试过但没有成功。然后我发现了这个
fun sumlists [] [] : int list = []
| sumlists (n :: ns) (m :: ms) = (n + m) :: sumlists ns ms;
;;; ML WARNING - Clauses of function declaration are non-exhaustive
;;; CONTEXT : fun sumlists
val sumlists = fn : int list -> int list -> int list
定义留下一个表达式,例如
sumlists [1] [];未定义。
所以我理解非穷举的概念,但我无法理解为什么我的函数是非穷举并给出错误的答案。
【问题讨论】:
标签: list sml decomposition