fun convert d = ((map (#1) d), (map (#2) d) );
This is basically trying to convert a list of pairs into a list of pairs. I also tried to declare the type of d as: (‘a *’b) list, but this leads to more errors.
I think this is related to The unknown size of tupple is related, you can use some help to understand it.
fun convert (d: ('a *'b) list) = ((map (#1) d), (map (#2) d) )
However, this is not a very good SML style. Use #1, #2, #n, etc. Waiting is a bit discouraged, because it will cause problems like this, you lose some of the common difficulties that type inference gives you.
On the contrary, you can define some explicit selection functions for pairs:
fun first (a, _) = a
fun second (_, b) = b
fun convert d = (map first d, map second d)
(Please note that I also deleted some extra parentheses from the conversion body, because function applications have a higher priority than tuple construction, and I also deleted the The semicolon is really needed to enter the REPL when you type the code or type the code)
This is a very good style guide for standard ML, from a course at Harvard University (or Tufts University). The The old version of the document clearly mentioned this in “Common Mistakes to Avoid”.
Avoid #1 and #2
Some beginners pick up the idea that a good way to get the second elemen t of a pair
p
is to write#2 p
.This style is not idiomatic or readable, and it can confuse the type checker. The proper way to handle pairs is by pattern matching, so
06002
is preferred, and not
06003
(For reasons I don’t want to discuss, these versions don’t even type-check.)
If your pair or tuple is not an argument to a function, use
val
to do the pattern matching:06004
But usually you can include matching in ordinary fun matching.
I always I am trying to create a function with a list of tuples as parameters, but I keep getting the error: “Unparsed flex record (need to know the names of all fields in this context)” My code is:
< p>
fun convert d = ((map (#1) d), (map (#2) d) );
This is basically an attempt to convert a list of pairs to A pair of lists. I also tried to declare the type of d as: (‘a *’b) list, but this resulted in more errors.
I think it has something to do with the unknown size of the tupple, you can use some help to understand it .
I suspect that the problem you encountered when annotating the d type is actually that you did not surround the annotation with parens, which worked for me:
< /p>
fun convert (d: ('a *'b) list) = ((map (#1) d), (map (#2) d) )
However, this is not a good SML style. Use #1, #2 ,#n and so on are a little discouraged, because it will cause problems like this, you lose some of the common difficulties that type inference gives you.
Instead, you can define some explicit selection functions for pairs:
fun first (a, _) = a
fun second (_, b) = b
fun convert d = (map first d, map second d)
(Please note that I also removed some extra parentheses from the transform body, because function applications have a higher priority than tuple construction, and I also removed The semicolon is really needed to enter the REPL when sorting the imperative code or typing the code)
This is a very good style guide for standard ML, from Harvard University (or Tufts University) The previous version of this document clearly mentioned this in “Common Mistakes to Avoid”.
Avoid #1 and #2
Some beginners pick up the idea that a good way to get the second element of a pair
p
is to write#2 p
.This style is not idiomatic or readable, and it can confuse the type checker. The proper way to handle pairs is by pattern matching, so
06002
is preferred, and not p>
06003
(For reasons I don’t want to discuss, these versions don’t even type-check.)
If your pair or tuple is not an argument to a function, use
val
to do the pattern matching:06004
But usually you can include mat ching in ordinary fun matching.