免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1509 | 回复: 1
打印 上一主题 下一主题

[函数] 【sets】erlang sets操作 [复制链接]

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-01-05 23:42 |只看该作者 |倒序浏览
  1. BASIC SET MANIPULATION WITH ERLANG

  2. This article deals with basic set manipulation in Erlang. If you are not fimilar with sets, perhaps now is a good time to do some reading on them here.

  3. Erlang Set Libraries

  4. Erlang offers 2 libraries for dealing with sets. The first one is sets and the second gb_sets. The sets library will generally work better for smaller sets while the gb_sets will work better for larger sets. The exact reason why this is is given here.

  5. In this article the gb_sets library will be used since this set will be primarally used later on.

  6. Example Problem

  7. This is the problem that will be used as an example of working with sets:

  8. A card is to be selected from a deck of 30 cards. The deck consists of 15 red cards numbered 1 to 15 and 15 black cards numbered 1 to 15. A card is to be choosen.

  9. What is the set that describes the outcome that the card is even, black, and less than 5?
  10. What is the set that describes the outcome that the card is greater than 5 and black?
  11. What is the set that describes the outcome that the card is even, black, or less than 5?
  12. What is the set that describes the outcome that the card is even and it is black or less than 5?
  13. What is the set that describes the outcome that the card is not even, not black and not less than 5?
  14. Theory First

  15. From the problem we need to first understand what sets are being asked for. This can be broken into three distinct sets:

  16. A: The event that the card is even.
  17. B: The event that the card is black.
  18. C: The event that the card is less than 5.
  19. So the answers described using A, B, and C are:

  20. A ∩ B ∩ C
  21. B ∩ Cc
  22. A ∪ B ∪ C
  23. A ∩ (B ∪ C)
  24. Ac ∩ Bc ∩ Cc
  25. Implementation

  26. Now the implementation in Erlang.

  27. The sample space is all of the cards, so in Erlang this can be represented as:

  28. gb_sets:from_list([{red, 1}, {red, 2}, {red, 3}, {red, 4}, {red, 5}, {red, 6},
  29.                    {red, 7}, {red, 8}, {red, 9}, {red, 10}, {red, 11}, {red, 12},
  30.                    {red, 13}, {red, 14}, {red, 15},
  31.                    {black, 1}, {black, 2}, {black, 3}, {black, 4}, {black, 5},
  32.                    {black, 6}, {black, 7}, {black, 8}, {black, 9}, {black, 10},
  33.                    {black, 11}, {black, 12}, {black, 13}, {black, 14}, {black, 15}]).
  34. The set A can be represented as:

  35. A = gb_sets:from_list([{red, 2}, {red, 4}, {red, 6}, {red, 8}, {red, 10}, {red, 12},
  36.                        {red, 14}, {black, 2}, {black, 4}, {black, 6}, {black, 8}, {black, 10},
  37.                        {black, 12}, {black, 14}]).
  38. The set B can be represented as:

  39. B = gb_sets:from_list([{black, 1}, {black, 2}, {black, 3}, {black, 4}, {black, 5},
  40.                        {black, 6}, {black, 7}, {black, 8}, {black, 9}, {black, 10},
  41.                        {black, 11}, {black, 12}, {black, 13}, {black, 14}, {black, 15}]).
  42. And the set C can be represented as:

  43. C = gb_sets:from_list([{red, 1}, {red, 2}, {red, 3}, {red, 4}, {red, 5},
  44.                        {black, 1}, {black, 2}, {black, 3}, {black, 4}, {black, 5}]).
  45. Answers

  46. After this point getting the answers is as simple as using the intersection, subtract, and union functions of the gb_sets library. The only trick is that the complement is the equivalent of a subtract from the sample space S.

  47. % 1:
  48. gb_sets:intersection([A, B, C]).

  49. % 2:
  50. gb_sets:intersection(B, gb_sets:subtract(S, C)).

  51. % 3:
  52. gb_sets:union([A, B, C]).

  53. % 4:
  54. gb_sets:intersection(A, gb_sets:union(B, C)).

  55. % 5:
  56. gb_sets:subtract(S, gb_sets:intersection([A, B, C])).
  57. The last answer uses De Morgan's laws, specifically that (A ∪ B)c = Ac ∩ Bc.
复制代码

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
2 [报告]
发表于 2015-01-05 23:43 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP