简体   繁体   中英

list to array without using array module in OCaml

How can I convert list to an array without using array module?

let rec mk_hlp sv = match s with |0 -> [] |_ -> v::mk_hlp (s-1) v;; let mk sv = [|mk_hlp sv|];;

My attempt, but returns array of list.

This is just a suggestion, but it seems to me the assignment is probably to reproduce the functionality of arrays rather than to create arbitrary OCaml arrays without using functions in the Array module. This latter seems a fairly specious assignment (no offense to anybody involved). You're going to have to use something that creates an array (such as an array constant [| ... |] ) and this for all practical purposes is really just another function in the Array module.

If instead you want to reproduce the functionality of arrays you can look for various other methods. It depends on what you are allowed to use. You can reproduce arrays at some level with just first-class functions and an if statement it seems to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM