I found the old U of Washington course CSE341: Programming Languages and am trying to follow along just so I can one day tackle Paulson's ML for the Working Programmer. So I have this:
fun number_in_month5 (m : int, ms : (int*int*int) list) =
List.length (List.filter (fn (_,x2,_) => x2 = m) ms)
which gives me back a list of matching 3-tuples. I can throw a List.length
on the front and get the count. But what about this
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
Testing gives bad answers sometimes
- number_in_month6 (2,[(2018,2,1),(2018,2,2),(2018,1,3),(2018,2,4)])
val it = 2 : int
Why is this not working? But then I have no skill in reading an ML error message. Somebody advised not declaring types at the start of the function -- and I tried and failed to do this. Then I found this
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
The definition leaves an expression such as
sumlists [1] [];
undefined.
So I understand the concept of non-exhaustive, but I can't fathom why my function is non-exhaustive and giving bad answers.