问题
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