Using Lists/Pattern Matching in SML/NJ
I'm having trouble with pattern matching with lists in SML. I'm trying to create a function that takes a 2x2 real matrix (defined as 'a list list
) and creates a complex (real * real)
. The matrix is formatted as a list of lists(that are made with reals) with each list being a row. I know that I have to pattern match but I开发者_如何学编程'm unsure how to implement my understanding into actual code. My code thus far is:
fun fromMatrix ((a::M):real matrix) : complex = (hd a, tl M);
I keep getting this error:
stdIn:1.5-13.32 Error: right-hand-side of clause doesn't agree with function result type [tycon mismatch]
expression: real * real list list
result type: complex
in declaration:
fromMatrix =
(fn <pat> :: <pat> : real matrix => (hd <exp>,tl <exp>): complex)
Okay so if (a::M)
has type real matrix
(or real list list
), then that means a
(head) has type real list
and M
(tail) has type real list list
. Then hd a
has type real
, and tl M
has type real list list
. So putting them together, (hd a, tl M)
has type real * real list list
, probably not what you want.
You probably want to understand that for lists, x :: y
means that x
is the first element, and y
is the rest of the list (not the second element), which is a list. Similarly, the hd
function returns the first element of a list, and the tl
function returns the rest of the list. If you want to extract the first two elements, you could use the pattern x :: y :: z
(where z
is the rest of the list after the first 2 elements). If you know it's going to be a 2-element list, you could match x :: y :: []
, or equivalently, [x, y]
. You can nest patterns, so if you have a 2-element list of 2-element lists, you could directly match [[a, b], [c, d]]
. However, using a fixed-size list is a sign of bad design. You probably want to use a tuple instead.
精彩评论