Hacker News new | past | comments | ask | show | jobs | submit login

I am usually not one for dismissing a language for syntax alone, but, the syntax for arrays versus lists feels very cumbersome. Can an experienced OCaml developer tell me how common it is to use lists versus arrays?

I can only imagine how many bugs I would type at this stage of my career after being used to [] for arrays but forgetting the pipe, and getting a list instead. It's also very difficult to type.




> I can only imagine how many bugs I would type at this stage of my career after being used to [] for arrays but forgetting the pipe, and getting a list instead.

Luckily, the compiler would catch this anywhere you tried to use a list where an array was expected.

> how common it is to use lists versus arrays?

Lists are far more common in day-to-day usage. The language even has native syntax for list consing and destructuring, which is useful for pattern matching.

OCaml arrays do have their place though, if you have a fixed number of items and need random access. Float arrays are also specialized, their elements are unboxed and contiguous which helps with performance for certain types of number-crunching applications.


> need random access.

I was thinking quite the opposite, I admit I don't know much about OCaml yet but I was very interested in trying ReasonML. Anyways, I would think you would use an array when you need sequential access and potentially help locality of reference when doing some number crunching? OCaml arrays are random access?


By random-access he means O(1) indexing, unlike lists. Yes, arrays works the same(but there is bounds-checking).


> you would use an array when you need sequential access and potentially help locality of reference when doing some number crunching

Yes, that's what float arrays were specialized for. From a quick glance at the OCaml compiler code, you might also get effective locality with other unboxed types (ints, chars). Boxed types will likely require an additional lookup even if the values are allocated contiguously. But if you're at the point of being concerned with memory access patterns, then bigarrays will probably work better.

If all you need is sequential access, then a list is much more convenient to use.

I'm not sure how all this translates to ReasonML though, as the JS runtime is very different.


Well, any bugs you would get are very immediate (due to the strong static typing). As for your other point, lists are fairly rare. You only need arrays when you need arbitrary indexing into a list. Most of the time, however, you're simply iterating through the list in some fashion.


Do you mean arrays are rare?


Whups, my bad. Arrays are rare, lists are very common.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: