0

There are two functions; funA and funB, respectively. a.i, a.o, ah, w, c are arrays in the function funA. The function funA shall be passed as a functional parameter to the function funB and the arrays should be able to be accessed by the function funB. Unfortunately, the syntax checker encountered an error "Error: Unbound record field a". Please comment, how to declare functional parameters in OCaml/ReasonML?

Full list

module Test = {
    let vector = Array.init;
    let matrix = (m, n, f) => vector(m, i => vector(n, f(i)));
    let length = Array.length;
    let rand = (x0, x1) => x0 +. Random.float(x1 -. x0);
    let funA = (ni, nh, no) => {
      let init = (fi, fo) => {
        let i = matrix(ni + 1, nh, fi);
        let o = matrix(nh, no, fo);
        ();
      };

      let a = {
        let i = vector(ni + 1, _ => 1.0);
        let o = vector(no, _ => 1.0);
        ();
      };

      let ah = vector(nh, _ => 1.0);
      let w = init((_, _) => rand(-0.2, 0.4), (_, _) => rand(-2.0, 4.0));
      let c = init((_, _) => 0.0, (_, _) => 0.0);
      ();
    };

    let funB = (net, inputs) => {
      let (ni, nh, no) = (
        length(net.a.i),
        length(net.ah),
        length(net.a.o),
      );
      ();
    };
  };
madeinQuant
  • 1,721
  • 1
  • 18
  • 29
  • Where are the records defined? A record needs to be in scope in order to be used, which you can do either by annotating the type or by opening the containing module where it's used, so that the type can be inferred from the fields used. – glennsl Mar 24 '20 at 08:54
  • @glennsl , I have no idea how to define records which to be used in this case. Is it same as struct in C programming? Would you suggest a link to me for reference. – madeinQuant Mar 24 '20 at 09:02
  • 1
    https://caml.inria.fr/pub/docs/manual-ocaml/coreexamples.html#s:tut-recvariants – glennsl Mar 24 '20 at 09:07
  • 1
    Or in more of a tutorial form: https://dev.realworldocaml.org/records.html – glennsl Mar 24 '20 at 09:07
  • 1
    Or for reason specifically (which I forgot this question was about): https://reasonml.github.io/docs/en/record – glennsl Mar 24 '20 at 09:09
  • All are good resources you should try to refer to before asking questions. – glennsl Mar 24 '20 at 09:10
  • I have already seen the resources before ask a question. From a coding design point of view, I prefer to pass a function over pass a type/record as a parameter of funB. – madeinQuant Mar 24 '20 at 11:20

1 Answers1

0

To resolve the functional parameter funA which is inaccessible in the function funB, apply the following type at the beginning of the module.

module Test = {

    type io('a) = {
      i: 'a,
      o: 'a,
    };

    type vec = array(float);

    type mat = array(vec);

    type funA = {
      a: io(vec),
      ah: vec,
      w: io(mat),
      c: io(mat),
    };

.......
madeinQuant
  • 1,721
  • 1
  • 18
  • 29