在 OCaml 中操作列表
我在以下上下文中操作 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用引用来打破持久性——因为函数式编程使用持久性数据。 在 myList 的声明中使用
ref
关键字:要取消引用列表,请使用
!
,因此有问题的行变为我建议您使用累加器,因为它符合函数式编程风格,如下所示:
编辑:
我没有编译我的代码,也没有注意到您使用了错误的符号来更改引用的值。 正确的符号是
:=
。 请参阅上面我的更改。 不过,我强烈建议您避免引用,并走累加器路线。You have to use references to break persistence --since functional programming uses persistent data. Use the
ref
keyword in the declaration of myList:To dereference the list use
!
, so the line in question becomesI suggest you use an accumulator as it's in the spirit of the functional-programming style, like this:
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.