1和Lisp之间有什么区别? [英] What is the difference between 1 and '1 in Lisp?

查看:146
本文介绍了1和Lisp之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有想过一个符号是否可以是Lisp中的一个数字,所以我今天玩了:

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

上面的代码是scheme,在Common Lisp和Clojure中也大致相同。 之间有什么区别吗?

解决方案

>非常不同。 '1 (quote 1)完全相同。 (car''x)评估符号quote。



/ code>是一个S表达式,它是一个数据的外部表示,数字1.要说 1 是一个'数字对象'输入该对象的S表达式都是可接受的。通常, 1 是实际数字对象的外部表示。



(quote 1)是另一个S表达式,它是一个列表的S表达式,其第一个元素是符号quote,其第二个元素是数字1。这是已经不同的句法关键字,不像函数,不被认为是语言中的对象,他们不评价他们。



但是,两者都是外部表示对同一个数据进行求值的对象(数据)。外部表示为 1 的数字,但它们最肯定不是相同的对象,相同的代码,相同的基准相同的任何东西,他们只是评价为相同事情。数字自己评估。要说他们是一样的是说:

 (+ 1(* 3 3))

 如果字符串是真的(* 5( -  5 3))字符串不是真的这是一个错误!)

是相同的,他们不是,他们都是不同的程序,只是发生在终止相同的值,lisp形式也是一个程序,



此外,我被教导了一个方便的技巧,这表明自我评估数据在输入时真的不是符号:

 (let((num 4))
(symbol?num); ====>求值为#f
(symbol?'num); ====>计算为#t
(符号?'4); ====>计算为#f
?##\c); #f再次,等等
(symbol?(car''x)); #t
(symbol?quote);错误,在大多数实现中

自我评估数据真正评估自身,它们不是某种类型的预定义符号。 / p>

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?

解决方案

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.

这篇关于1和Lisp之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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