edeita2
04-11-2004, 08:15 PM
I just do this practice by myself independently to get to know some ML. I did this problem in a different way: Create an array with 10 elements each element equal to its index +2, write a function that takes the list and returns a tuple of even indexed elements. My solution:
- val elements = Array.tabulate(10, fn x => x + 2);
val elements = [|2,3,4,5,6,7,8,9,10,11|] : int array
- val elements = Array.tabulate(10, fn x => (x + 2) mod 2 = 0);
val elements = [|true,false,true,false,true,false,true,false,true,false|]
: bool array
How can I write a function to print the even elements only? I thought something like this:
-fun even(elements : Array) = elements mod 2;
stdIn:18.6-18.11 Error: unbound type constructor: Array
didn`t I bind that array above to "elements"?
thanks
- val elements = Array.tabulate(10, fn x => x + 2);
val elements = [|2,3,4,5,6,7,8,9,10,11|] : int array
- val elements = Array.tabulate(10, fn x => (x + 2) mod 2 = 0);
val elements = [|true,false,true,false,true,false,true,false,true,false|]
: bool array
How can I write a function to print the even elements only? I thought something like this:
-fun even(elements : Array) = elements mod 2;
stdIn:18.6-18.11 Error: unbound type constructor: Array
didn`t I bind that array above to "elements"?
thanks