Question

I wrote this f# code to create a jagged (non-rectangular) array.

open System

type MatrizIrregular() = 
  let irregular = Array2D.zeroCreate 5

  member This.Inicializar() =
    for f in 0 .. 4 do
      printf "Introduzcal el número de columnas de la matriz de la fila %d: " f
      let res = Console.ReadLine()
      let col = Int32.Parse(res)
      Array2D.set irregular f (col)
      for c in 0 .. col - 1 do
        printf "Introduzca el valor de la columna %d: " c
        let res2 = Console.ReadLine()
        Array2D.set irregular f c (Int32.Parse(res2))

  member This.Mostrar() = 
    for f in 0 .. 4 do
    for f in 0 .. irregular.[f,c].Lenght - 1 do
      printf "%d " irregular.[f,c]
      printf "\n" 

let matriz = MatrizIrregular()
matriz.Inicializar()
matriz.Mostrar()

I get errors on lines 11,15

This expression was expected to have type 'a[,] but here has type int -> 'b[,]

and on lines 19,20

The operator 'expr.[idx]' has been used on an object of indeterminate type based on information prior to this program point. Consider adding futher type constrains.

I want do something like this java code in f#

import java.util.Scanner;
public class MatrizIrregular {
    public Scanner teclado = new Scanner(System.in);
public int[][] irregular;

public void inicializar(){
    irregular = new int[5][];
    for(int f=0;f<irregular.length;f++){
            System.out.print("Introduzca el numero de columnas del la fila de la matriz "+f+": ");
    int col = teclado.nextInt();
    irregular[f] = new int[col];
    for(int c =0; c<irregular[f].length;c++){
        System.out.print("Introduzca el elemento de la columna "+c+": ");
            irregular[f][c] = teclado.nextInt();
            }
        }
    }

public void mostrar(){
    for(int f=0;f<irregular.length;f++){
            for(int c=0;c<irregular[f].length;c++){
               System.out.print(irregular[f][c]+" ");
            }
            System.out.print("\n");
        }
}

public static void main(String[] ar){
     MatrizIrregular matriz = new MatrizIrregular();
     matriz.inicializar();
     matriz.mostrar();
}

}

Was it helpful?

Solution

A 2D array is not irregular, so similar to java you need to declare an array of an array of ints. In F# that's int array array and zeroCreate needs to know what type it's going to initialize an array for. Also be careful of indentation, your nested for loops weren't indented properly.

open System

type MatrizIrregular() = 
  let irregular : int array array = Array.zeroCreate 5

  member This.Inicializar() =
    for f in 0 .. 4 do
      printf "Introduzcal el número de columnas de la matriz de la fila %d: " f
      let res = Console.ReadLine()
      let col = Int32.Parse(res)
      let cols : int array = Array.zeroCreate col
      for c in 0 .. col - 1 do
        printf "Introduzca el valor de la columna %d: " c
        let res2 = Console.ReadLine()
        Array.set cols c (Int32.Parse(res2))
      Array.set irregular f cols

  member This.Mostrar() = 
    for f in 0 .. 4 do
      for c in 0 .. (Array.length irregular.[f]) - 1 do
        printf "%d " irregular.[f].[c]
      printf "\n" 

let matriz = MatrizIrregular()
matriz.Inicializar()
matriz.Mostrar()

Not the most functional answer, but it is what was asked for.

OTHER TIPS

You can't make jagged 2D arrays like this. Array2D and the type [,] and can only be used for rectangular 2D arrays.

To see how to work with jagged arrays, take a look at this link.


I'd like to comment on your use of Array2D.zeroCreate. It has this type:

val it : (int -> int -> 'a [,])

When you have given it two integers, it'll give you an array. If you give it only one integer:

let irregular = Array2D.zeroCreate 5
val irregular : (int -> '_a [,])     (* Also complains about generic types. *)

You get a function that when given the missing second dimension will give you an array. You don't get an array with an unspecified second dimension.

(Maybe try and play with let f x y = x + y and f 1 in the interpreter; see what the types are and try to apply f to one or two things.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top