在使用了Mathematica的符号和数字功能之后,我发现它也是一种不错的编程语言。但是,使它作为通用语言的吸引力下降的原因是缺少类似于C的结构数据类型(或Pascal中已知的记录类型)。我该如何解决这个问题?
最佳答案
更新:Mathematica 10引入了Association
,它具有struct
的许多最重要的属性。 (请参见new answer。)以下是此答案的原始版本,该版本已过时。
您可以使用Mathematica规则列表来模拟类似C的结构数据类型。例如。,:
person = {firstName -> "John", lastName -> "Doe"}
然后,您可以使用
/.
运算符访问记录的字段:firstName /. person
产生
John
。lastName /. person
产生
Doe
。要更新记录的字段,请将更新的字段放在列表的前面:
PrependTo[person , firstName -> "Jane"]
firstName /. person
然后产生Jane
。另请参见transformation rules上的Mathematica文档。
关于struct - Mathematica中的结构数据类型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1453006/