℡Wang Yan 2011-02-02 13:00 采纳率: 100%
浏览 483
已采纳

Lisp 与 Erlang 原子,Ruby 与方案符号。 它们有多大用处?

How useful is the feature of having an atom data type in a programming language?

A few programming languages have the concept of atom or symbol to represent a constant of sorts. There are a few differences among the languages I have come across (Lisp, Ruby and Erlang), but it seems to me that the general concept is the same. I am interested in programming language design, and I was wondering what value does having an atom type provide in real life. Other languages such as Python, Java, C# seem to be doing quite well without it.

I have no real experience of Lisp or Ruby (I know the syntaxes, but haven't used either in a real project). I have used Erlang enough to be used to the concept there.

转载于:https://stackoverflow.com/questions/4874744/lisp-and-erlang-atoms-ruby-and-scheme-symbols-how-useful-are-they

  • 写回答

13条回答 默认 最新

  • 谁还没个明天 2011-02-02 13:04
    关注

    A short example that shows how the ability to manipulate symbols leads to cleaner code: (Code is in Scheme, a dialect of Lisp).

    (define men '(socrates plato aristotle))
    
    (define (man? x) 
        (contains? men x))
    
    (define (mortal? x) 
        (man? x))
    
    ;; test
    
    > (mortal? 'socrates)
    => #t
    

    You can write this program using character strings or integer constants. But the symbolic version has certain advantages. A symbol is guaranteed to be unique in the system. This makes comparing two symbols as fast as comparing two pointers. This is obviously faster than comparing two strings. Using integer constants allows people to write meaningless code like:

    (define SOCRATES 1)
    ;; ...
    
    (mortal? SOCRATES)
    (mortal? -1) ;; ??
    

    Probably a detailed answer to this question could be found in the book Common Lisp: A Gentle Introduction to Symbolic Computation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示