local-host 2010-06-03 14:15 采纳率: 100%
浏览 215
已采纳

1和'1在 Lisp 中的区别是什么?

I had never really thought about whether a symbol could be a number in Lisp, so I played around with it today:

> '1
1
> (+ '1 '1)
2
> (+ '1 1)
2
> (define a '1)
> (+ a 1)
2

The above code is scheme, but it seems to be roughly the same in Common Lisp and Clojure as well. Is there any difference between 1 and quoted 1?

转载于:https://stackoverflow.com/questions/2966765/what-is-the-difference-between-1-and-1-in-lisp

  • 写回答

7条回答 默认 最新

  • csdnceshi62 2010-06-04 17:49
    关注

    Well, they are in fact very different. '1 is however precisely the same as (quote 1). (car ''x) evaluates to the symbol 'quote'.

    1 is an S-expression, it's the external representation of a datum, a number 1. To say that 1 is a 'number-object' or an S-expression to enter that object would both be acceptable. Often it is said that 1 is the external representation for the actual number object.

    (quote 1) is another S-expression, it's an S-expression for a list whose first element is the symbol 'quote' and whose second element is the number 1. This is where it's already different, syntactic keywords, unlike functions, are not considered objects in the language and they do not evaluate to them.

    However, both are external representations of objects (data) which evaluate to the same datum. The number whose external representation is 1, they are however most certainly not the same objects, the same, code, the same datum the same whatever, they just evaluate to the very same thing. Numbers evaluate to themselves. To say that they are the same is to say that:

    (+ 1 (* 3 3))
    

    And

    (if "Strings are true" (* 5 (- 5 3)) "Strings are not true? This must be a bug!")
    

    Are 'the same', they aren't, they are both different programs which just happen to terminate to the same value, a lisp form is also a program, a form is a datum which is also a program, remember.

    Also, I was taught a handy trick once that shows that self-evaluating data are truly not symbols when entered:

    (let ((num 4))
      (symbol? num) ; ====> evaluates to #f
      (symbol? 'num) ; ====> evaluates to #t
      (symbol? '4) ; ====> evaluates to #f
      (symbol? '#\c) ; #f again, et cetera
      (symbol? (car ''x)) ; #t
      (symbol? quote) ; error, in most implementations
    )
    

    Self evaluating data truly evaluate to themselves, they are not 'predefined symbols' of some sorts.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题