Reputation: 5
The function "total" should take a list of tuples and return a list of the sum of the integers in each tuple. For example, total [(15, 15, 15), (14, 13, 15), (13, 12, 14)] returns [45, 42, 39].
fun help (i1,i2,i3) = i1+i2+i3
fun total stuff = map help stuff
total [(15, 15, 15), (14, 13, 15), (13, 12, 14)]
The error is:
! fun total stuff = map help stuff
! ^^^^^^^^^^^^^^
! Type clash: expression of type
! 'a list
! cannot have type
! 'b -> 'c
Upvotes: 0
Views: 678
Reputation: 51998
If I try to paste your code directly as is into SML/NJ's REPL I get an error similar to but different from the one that you get (you seem to be using another implementation of ML). The problem seems to be that the REPL has trouble telling where the definition of total
ends, interpreting the next line as part of the definition. A simple fix is to put semicolons at the end of the lines before pasting into the REPL so that SML can figure out that
val help = fn : int * int * int -> int
val total = fn : (int * int * int) list -> int list
val it = [45,42,39] : int list
Alternatively, you could replace your final line by something like
val test = total [(15, 15, 15), (14, 13, 15), (13, 12, 14)];
I'm just guessing here -- but it seems like maybe you are using cut and paste to transfer definitions from a text file into a REPL. If so, that can be unreliable as your problem indicates. A work-flow that I hit upon is to create a text file (if you are in Windows I recommend textpad since it has decent SML syntax highlighting which can be optionally installed) and then, at the top of the file, include a comment line like
(*use "C:/programs/experiments.sml";*)
(or whatever you choose to call your file) and then, after saving, just paste the text inside the comment into the REPL
Upvotes: 1