OCaml's Pointer

3/4/2021

The pointer in OCaml is different from what it is in C-like languagues, which made it harder to understand when comes to labs or projects.

This example, which is a simple enqueue function, has a few things to care about:

Enqueue

1. Turns out that "left", "right" are just pointers to the variable, or pointers to the node, instead of node itself. In a C-like language like java, I can simply say ptr is the pointer, but also the node itself, as I can call node function on ptr. But in OCaml, pointer is pointer, and node is node, represented as !left in this example.

2. In line 27, left := newNode is not re-assigning the node to left, as I was thinking, but to set the left pointing to the newNode. It may seems hard/easy to understand, but consider left now is still not the newNode, but the pointer to that node, thus would change the node in q as the left is now pointing to some other node.

After that it's just normal easy stuff, I'll put the code up for this queue, just incase sometime I may want to check it out.

Queue-lab6

Until next time;;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容