Mathematica 中的结构数据类型? [英] Struct data type in Mathematica?

查看:38
本文介绍了Mathematica 中的结构数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用了 Mathematica 的符号和数字功能之后,我发现它也是一种不错的编程语言.然而,使它作为通用语言不那么吸引人的原因是缺少类似 C 的结构数据类型(或 Pascal 中已知的记录类型).我该如何解决这个问题?

After playing with Mathematica's symbolic and numerical capabilities, I find it to be a decent programming language, too. However, something making it less appealing as a general-purpose language is the lack of C-like struct data type (or the record type as known in Pascal). How can I get around this problem?

推荐答案

更新:Mathematica 10 引入了 Association,它具有 struct 的许多最重要的属性.(请参阅新答案.)下面是这个答案的原始版本,有些不推荐使用.

Update: Mathematica 10 has introduced Association, which has many of the most important properties of a struct. (See new answer.) The original, somewhat deprecated version of this answer is below.

您可以使用 Mathematica 规则列表来模拟类似 C 的结构数据类型.例如:

You can use a Mathematica rule lists to mimic a C-like struct data type. E.g.,:

person = {firstName -> "John", lastName -> "Doe"}

然后您可以使用 /. 运算符访问记录的字段:

You can then access the record's fields by using the /. operator:

firstName /. person

产生John.

lastName /. person

产生Doe.

要更新记录的字段,请将更新的字段添加到列表中:

To update a field of a record, prepend the updated field to the list:

PrependTo[person , firstName -> "Jane"]

名字/.person 然后产生 Jane.

另请参阅关于转换规则的Mathematica文档一>.

Also see the Mathematica documentation on transformation rules.

这篇关于Mathematica 中的结构数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆