Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Ml > Help with index...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 867 of 973
Post > Topic >>

Help with indexing into new recursive datatype

by John Sasso <jsassojr@[EMAIL PROTECTED] > Apr 5, 2007 at 08:56 PM

Hi,

For a functional programming course I am taking, I am practicing
defining new datatypes with SML.  In particular, I defined a
multidimensional array type:

datatype 'a array = Array of (int * 'a list);

where the int represents the dimension of the outermost array.  Examples:

val vector = Array(3, [1,2,3]);
val matrix = Array(3,
               [Array(3, [1, 2, 3]),
                Array(3, [4, 5, 6]),
                Array(3, [7, 8, 9])]  )
val cub = Array(2,
          [Array(3,
                 [Array(3,
                        [Array(3, [1, 2, 3]), Array(3, [4, 5, 6]),
                         Array(3, [7, 8, 9])]),
                  Array(3,
                        [Array(3, [1, 2, 3]), Array(3, [4, 5, 6]),
                         Array(3, [7, 8, 9])]),
                  Array(3,
                        [Array(3, [1, 2, 3]), Array(3, [4, 5, 6]),
                         Array(3, [7, 8, 9])])]),
           Array(3,
                 [Array(3,
                        [Array(3, [1, 2, 3]), Array(3, [4, 5, 6]),
                         Array(3, [7, 8, 9])]),
                  Array(3,
                        [Array(3, [1, 2, 3]), Array(3, [4, 5, 6]),
                         Array(3, [7, 8, 9])]),
                  Array(3,
                        [Array(3, [1, 2, 3]), Array(3, [4, 5, 6]),
                         Array(3, [7, 8, 9])])])])

matrix is a 3x3 array, and cube is a 2x3x3x3 array

I wanted to create a function that will return the element of the array
indexed by an int list.  For example, for vector, index [2] means get
the 2nd element; for matrix, index [2,3] means get the element in the
2nd row and 3rd column.  Etc...

So in an attempt to create such a function, I started off small to try
and create a function to index into a 2-D array:

fun matrixidx(i::nil, Array(d,a)) =
       if i = 1 then
          hd(a)
       else
          matrixidx([i], Array(d,tl(a)) )
|   matrixidx(i::ls, Array(d,a)) =
       if i = 1 then
          matrixidx(ls, hd(a))
       else
          matrixidx((i-1)::ls, Array(d,tl(a)) );


But I get the following error:


! Toplevel input:
!           matrixidx(ls, hd(a))
!                            ^
! Type clash: expression of type
!   'a list
! cannot have type
!   'a array/4 list
! because of circularity


I've been spending hours on this and tried many different variations in
defining matrixidx, but no-go.  I am very confused and do not know what
the problem is, or even how to fix it.  Thanks!

--john
 




 3 Posts in Topic:
Help with indexing into new recursive datatype
John Sasso <jsassojr@[  2007-04-05 20:56:51 
Re: Help with indexing into new recursive datatype
Jon Harrop <jon@[EMAIL  2007-04-06 23:40:08 
Re: Help with indexing into new recursive datatype
jmv16@[EMAIL PROTECTED]   2007-04-10 16:09:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Oct 10 20:11:10 CDT 2008.