在 OCaml 中操作列表

发布于 2024-07-14 19:45:37 字数 1357 浏览 6 评论 0原文

我在以下上下文中操作 OCaml 中的深层嵌套列表时遇到问题。

class foo (pIn:int)=
object (self)
    val p = pIn
    val even = if (pIn mod 2) = 0 then true else (false)
    method doIt = "doIt"
    method isEven = even
    method getP = p
end;;

let rec createListOfElements howMany =  (
    Random.self_init ();
    if howMany > 1 then ((new foo (Random.int 10))::(createListOfElements (howMany - 1)))
    else ([(new foo (Random.int 10))])  );;

let myList = createListOfElements 5;;

let rec process1 param =
     if param <= 10 then
          let f = new foo param in          (
                if f#isEven then (myList <- List.append myList (createListOfElements f#getP));
                Printf.printf "%s\n" f#doIt;
                process1 (param+1)                                  )
in process1 0;;

我得到的错误是“未绑定实例变量 myList”。 在这种情况下,我该如何将“List.append myList (createListOfElements f#getP)”的结果分配给 myList?

谢谢!


编辑函数:

let myList = ref (createListOfElements 5);;

let rec process1 param =
     if param <= 10 then
          let f = new foo param in          (
                if f#isEven then (myList <- !myList @ (createListOfElements f#getP));
                Printf.printf "%s\n" f#doIt;
                process1 (param+1)                                  )
in process1 0;;

I am having issues manipulating deeply nested lists in OCaml in the below context.

class foo (pIn:int)=
object (self)
    val p = pIn
    val even = if (pIn mod 2) = 0 then true else (false)
    method doIt = "doIt"
    method isEven = even
    method getP = p
end;;

let rec createListOfElements howMany =  (
    Random.self_init ();
    if howMany > 1 then ((new foo (Random.int 10))::(createListOfElements (howMany - 1)))
    else ([(new foo (Random.int 10))])  );;

let myList = createListOfElements 5;;

let rec process1 param =
     if param <= 10 then
          let f = new foo param in          (
                if f#isEven then (myList <- List.append myList (createListOfElements f#getP));
                Printf.printf "%s\n" f#doIt;
                process1 (param+1)                                  )
in process1 0;;

The error I get is, "Unbound instance variable myList". How do I go about assigning the result of "List.append myList (createListOfElements f#getP) to myList in this context?

Thanks!


Edited function:

let myList = ref (createListOfElements 5);;

let rec process1 param =
     if param <= 10 then
          let f = new foo param in          (
                if f#isEven then (myList <- !myList @ (createListOfElements f#getP));
                Printf.printf "%s\n" f#doIt;
                process1 (param+1)                                  )
in process1 0;;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难愈 2024-07-21 19:45:37

您必须使用引用来打破持久性——因为函数式编程使用持久性数据。 在 myList 的声明中使用 ref 关键字:

let myList = ref (createListOfElements 5)

要取消引用列表,请使用 !,因此有问题的行变为

if f#isEven then
  myList := !myList @ f#getP;

我建议您使用累加器,因为它符合函数式编程风格,如下所示:

let rec process1 lst = function
  | x when x <= 10 ->
      let f = new foo x in
      if f#isEven then
          process1 (lst @ (createListOfElements f#getP)) (param+1)
      else
          process1 lst (param+1)
  | _ -> lst

编辑:

我没有编译我的代码,也没有注意到您使用了错误的符号来更改引用的值。 正确的符号是:=。 请参阅上面我的更改。 不过,我强烈建议您避免引用,并走累加器路线。

You have to use references to break persistence --since functional programming uses persistent data. Use the ref keyword in the declaration of myList:

let myList = ref (createListOfElements 5)

To dereference the list use !, so the line in question becomes

if f#isEven then
  myList := !myList @ f#getP;

I suggest you use an accumulator as it's in the spirit of the functional-programming style, like this:

let rec process1 lst = function
  | x when x <= 10 ->
      let f = new foo x in
      if f#isEven then
          process1 (lst @ (createListOfElements f#getP)) (param+1)
      else
          process1 lst (param+1)
  | _ -> lst

EDIT:

I didn't compile my code and didn't notice that you are using the wrong symbol to change the value of the reference. The correct symbol is, :=. See my change above. I strongly suggest you avoid references, though, and go the accumulator route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文