Convert record to list

跟風遠走 提交于 2020-07-10 10:25:30

问题


Suppose

type pair_int = {l1:int; l2:int, ..., ln:int}
let test = {l1=2; l2=4, ..., ln=71}

I thought I could do something like map (fun (x,y) -> y) test, but it doesn't work

How can I get the list [2,4, ..., 71] from test?


回答1:


There's no nice way to do this inside the OCaml type system. You can't map over the fields of a record because they can be of all different types. Your type pair_int looks suspiciously like a list or an array already. The field names don't add any semantic content, and the fields are all the same type. You might consider just using a list or an array instead.



来源:https://stackoverflow.com/questions/62766331/convert-record-to-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!