将记录转换为列表 [英] Convert record to list
本文介绍了将记录转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设
type pair_int = {l1:int; l2:int, ..., ln:int}
let test = {l1=2; l2=4, ..., ln=71}
我以为我可以做 map(fun(x,y)-> y)test
之类的事情,但这不起作用
I thought I could do something like map (fun (x,y) -> y) test
, but it doesn't work
如何从 test
获得列表 [2,4,...,71]
?
推荐答案
在OCaml类型系统中没有很好的方法.您无法映射记录的字段,因为它们可以是所有不同的类型.您的类型 pair_int
可疑地看起来像一个列表或数组.字段名称不添加任何语义内容,并且字段都是相同的类型.您可能会考虑只使用列表或数组.
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.
这篇关于将记录转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文