数字,字符,字符串

Common lisp 中的数据类型,包括我们平常见的整数,复数,浮点数以及数组,列表,哈希表,输入和输出流,一种可移植地表示文件名的抽象。函数同样是数据类型,他可以保存在变量中,可作为实参传递,也可以作为返回值以及运行期创建。

1:数字

Lisp 是一门用于数学的良好语言,因为他最初就是由数学家设计的,用作研究数学函数的工具,它的数学更接近与数字数学,

Common lisp支持复数-通过在负数上获取平方根和对数所得到的结果。

Lisp读取器和lisp求值器的分工,读取器负责将文本转化为lisp对象,而lisp求值器只处理这些对象。

你可以有多种不同的字面表示方式,所有这些都将被lisp读取器转化为相同的对象,比如你可以将整数10写成10, 20/2,#xA任何形式但是最终显示的都是10

CL-USER> 10/2   5
CL-USER> 1/3    1/3
CL-USER> 10/4   5/2
CL-USER> #xa    10:

2:整数,浮点数,复数的规范化输出也就是无论你按照什么格式输入一些值,他都会按照一定的格式进行显示读取该对象,然后进行处理或者直接输出。

整数的规范化输出

整数的语法:可选符号(+-)默认是+,后接一个或多个数字

比值的语法:可选符号,代表分子的数位序列,斜杠(/),代表分母的数位序列

所有有理数(包括整数和分数)都以“简化”形式打印,整数值以整数语法打印,而比值被打印成分子和分母约到最简。同时如果你以其他进制键入的话,都要转化为十进制形式。

前缀#b/#B,按二进制来读取;#o/#O按八进制读取;#x/#X按十六进制读取。并且你可以自定义什么进制输入形式为#nR/#nr。其中n必须以236中的十进制数值。

这些进制必须应用到整个有理数(分数和整数),不能用一种进制写分子,另一进制写分母。

当其他进制的时候,注意键入时候上限字母,比如A=1016进制你可以输入的最大字母是F,如下23进制,如果你输入一个n23,它解析不出来是多少的,他最多解析到22=m

CL-USER> 123         123
CL-USER> +123        123
CL-USER> -123        -123
CL-USER> -4/6        -2/3
CL-USER> #o101       65
CL-USER> #23rfdn

; No value
CL-USER> #23rfd      358
CL-USER> #b1010/1110 5/7
CL-USER> #23rm       22

浮点数规范化输出

浮点数总共包含4中浮点数子类型:短型,单精度,双精度,长型。分别对应s f d l

浮点数的基本格式:可选符号,非空十进制数字序列,小数点,可能会接一个“科学计数法”指数标记(当是e,并且后面数字小于7是不需要写e了。),可选符号,数字序列。

默认情况下指数标记符号都是e,所以就不用写了。

如果e/d后面为正数,7为界限,如果这个数的值是小于e7这个级别的话,他就会把e原先所跟随的整数一起挪到整数部分。例如123e4,他就是e6数量级的,所以最后结果是原来的4也都挪到整数部分了。

如果e/d后面为负数,它是以-4为界限,当整个数字如果是大于-4的,同样不会再显示这种科学计数形式,并且注意如果就单纯的一个1,它的显示是1.e-4。点后没有0

最麻烦的就是浮点数的表示了,对于上面7/-4的界限,同样适用于其他浮点子类型,用更一般的方式说就是不显示科学计数法。

#b/#x等这些进制语法只会用到有理数上面。

CL-USER> 1e0       1.0
CL-USER> 123e0     123.0
CL-USER> 1.0       1.0
CL-USER> 1e0       1.0
CL-USER> 123e0     123.0
CL-USER> 123e3     123000.0
CL-USER> 12e10     1.2e11
CL-USER> 123e4     1230000.0
CL-USER> 123e5     1.23e7
CL-USER> 0.123e12  1.23e11
CL-USER> 0.123     0.123
CL-USER> 123d4     1230000.0d0
CL-USER> 123d5     1.23d7
CL-USER> 1e-1      0.1
CL-USER> 1e-3      0.001
CL-USER> 1e-4      1.e-4
CL-USER> 1e-6      1.e-6
CL-USER> 12e-5     1.2e-4

复数的规范化输出

格式:#c/#C 跟上一个或两个实数组成的序列,分别表示实部和虚部(必须同为有理数或为相同类型的浮点数)。

规范化格式(关键还是浮点数规范化能够掌握):

当是有理数和浮点数组成时:有理数转化为适当的浮点数。

当时不同表示法的浮点数时:小精度向大精度转化

当虚部的值为0时        :整数0的话,转化为对应整数,浮点0,转化相应类型浮点数

CL-USER> #c (2 1)         #C(2 1)
CL-USER> #c (1/2 3/5)     #C(1/2 3/5)
CL-USER> #c (3 7.0)       #C(3.0 7.0)
CL-USER> #c (2.0 1.0d0)   #C(2.0d0 1.0d0)
CL-USER> #c (3 0)         3
CL-USER> #c (3 0.0)       #C(3.0 0.0)
CL-USER> #c (-1/2 0)      -1/2
CL-USER> #c (1/2 0.0)     #C(0.5 0.0)

3:基本运算:

* It takes any number of arguments. When given no arguments it returns 1.

expects at least one argument. A call of the form (/ n) is equivalent to (/ 1 n), it will return a ratio if the first is not a multiple of the second.

CL> 1.0/5           ; No value The variable |1.0/5| is unbound.
CL> 4/6             2/3
CL> (/ 1.0 3.4)     0.29411763
CL> (/ 1 3.0)       0.33333334 而不是1/3
CL> (float 365/12)  30.416666

Exponentiation

Xn    --> (expt x n) 幂函数。比如x2, x1/2

ex    --> (exp 2)    

log  --> (log x n)第二个参数是base,默认是自然对数即lnx 

CL> (expt 2 5)                  32
CL> (expt 4 1/2)                2.0
CL> (sqrt 4)                    2.0 针对开方比expt快
CL> (exp 2)                     7.389056
CL> (log (exp 2))               2.0
CL> (log (expt 10 2) 10)        2.0

因为符号/不提供截断处理,Common Lisp提供了4中类型的截断和舍入用于将一个实数(有理数和浮点数)转化为整数。他们都是最总的结果为整数。

Floor     向负无穷方向截断,返回小于或者等于实参的最大整数。

Ceiling   向正无穷截断。

Truncate 0截断,对于正实数相当于Floor,负实数相当于Ceiling

Round   舍入最接近整数,如果刚好位于整数之间,它去离得最近的整数。

Mod     

Rem    

CL> (truncate 1.34)
1
0.34000003

The difference of . 00000005 is due to the inherent (固有的)inexactitude(不精确) of floating point computation.

Like truncate, floor and ceiling round also return as a second value the difference between the argument and the first return value

(defun our-truncate (n)
	(if (> n 0)
	(floor n)
	(ceiling n)))

When the argument is equidistant from two integers, Common Lisp, like many programming languages, does not round up. Instead it rounds to the nearest even digit

CL> (round 5.6)
6
-0.4000001
CL-USER> (mapcar #'round '(-2.5 -1.5 1.5 2.5))	 
(-2 -2 2 2)

signum returns either 1, 0, or - 1 , depending on whether its argument is positive, zero, or negative.侧面反应它是正负。但是-0.0注意

abs      returns the absolute value of its argument. Thus (* (abs x) (signum x)) =x.

CL> (mapcar #'signum '(-2 -0.0 0.0 0 .5 3))
(-1 -0.0 0.0 0 1.0 1)

Ratios and complex numbers are conceptually two-part structures.

numerator and denominator return the corresponding components of a ratio or integer. (If the number is an integer, the former returns the number itself and the latter returns 1.) 

realpart and imagpart return the real and imaginary components of any number. (If the number isn't complex, the former returns the number itself and the latter returns zero.)

CL> (mapcar #'numerator '(5/6  6 -5/6 ))
(5 6 -5)
CL-USER> (mapcar #'denominator '(5/6  6 -5/6 ))
(6 1 6)
CL-USER> (mapcar #'realpart '( 4 #c(1 4) #c(4.3 -5.2)))
(4 1 4.3)
CL-USER> (mapcar #'imagpart '( 4 #c(1 4) #c(4.3 -5.2)))
(0 4 -5.2)

Random 返回一个比他小的整数或者浮点数,类型由n来决定

CL-USER> (mapcar #'random '(3 1.0))
(2 0.2647184)

4 :数值比较

Operator is = , /= ,< ,<= ,> ,>= 。其中 是数字等价,即equalp. is strict than eql

CL> (= 1 1.0)
T
CL> (eql 1 1.0)
NIL

(<= w x y z) is equivalent to: the conjunction of a binary operator applied to successive pairs of arguments等价于把操作符应用于后续的每对参数的组合。比如只要下面式子成立,就自然而然可以退出w <=后面所有的数,x <=后面所有的数。

(and (<= w x) (<= x y) (<= y z))

但是对于/=,你只能是都比较了,才能说明他们之间都互不相等。

(/= w x y z)  is equivalent to :

(and (/= w x) (/= w y) (/= w z)

(/= x y) (/= x z) (/= y z))

下面函数用于测试单一实数

ZEROP  是否等于0

MINUSP 是否小于0

PLUSP  是否大于0

EVENP  是否为偶

ODDP   是否为奇

Note to minusp/zerop:

CL> (list (minusp -0.0) (zerop -0.0)) ;;虽然0.0前面有符号但是仍旧是0
(NIL T)

因为本身数值进行比较的时候都是用<,>,>=等之类的符号,而对于字符和字符串的话,需要有专门的函数进行处理

5:字符比较

数值相似物

大小写相关

大小写不相关

=

char=

char-equal

/=

char/=

char-not-equal

<

char<

char-lessp

>

char>

char-greaterp

<=

char<=

char-not-greaterp

>=

char>=

char-not-lessp


 6:字符串比较

数值相似物

大小写相关

大小写不相关

=

string=

string-equal

/=

string/=

string-not-equal

<

string<

string-lessp

>

string>

string-greaterp

<=

string<=

string-not-greaterp

>=

string>=

string-not-lessp

CL-USER> (equal "fred" "fred")         T
CL-USER> (equal "fred" "Fred")         NIL
CL-USER> (equalp "fred" "Fred")        T
CL-USER> (string= "fred" "Fred")       NIL
CL-USER> (string-equal "fred" "Fred")  T

注意点:

1:字符串进行比较它默认一些关键字参数:start1 , :start2 , :end1 , :end2,因为这些操作符只是针对两个对象处理的。

2:对于那些只有当串不一样返回真的函数而言,即除string=string-equal外,表达式的值是第一个串中首次检测到不匹配的索引(索引是从0开始计算的)。因为剩下的这些函数,一个个字符匹配的话,前面无论有多少一样的字符,他们两个之间第一个不一样的才是决定结果是NIL还是真的关键。在这里,他不是简简单单的返回T,而是要把第一个串中第一个不一样的序列显示出来。比如下面例子(string< "family" "lisp")==>0;因为比较两个串关键是看第一个不一样的字符。F并且F<L,并且索引为0,故返回0,并隐含结果为真。这时如果把他两个颠倒过来(string< "lisp" "family")。返回为Nil,同样是比较第一个不一样的,但是L>f,说明结果为NIL,然后就没有然后了。因为都已经假了,你还想怎么的。

为啥只是针对那些只有当不一样的时候才为真的的函数呢?因为你现在假设string=也同样返回一个第一个串中不匹配的序列,"lisp" "lisper"它本来应该是NIL,但是你现在给他返回一个4的话,竟然成真了;把握一个分寸,不能改变人家真假的情况下。因为如果值不是NIL就表示这个判断是真的,即使返回值为0也是真。

CL-USER> (string/= "lisp" "lisper")     4
CL-USER> (string/= "lisp" "lisp")       NIL
CL-USER> (string/= "family" "lisp")     0
CL-USER> (string/= "family" "fun")      1
CL-USER> (string< "lisp" "lisper")      4
CL-USER> (string< "lisper" "lisp")      NIL
CL-USER> (string< "family" "fun")       1
CL-USER> (string< "family" "lisp")      0
CL-USER> (string< "lisp" "family")      NIL
CL-USER> (string> "lisp" "family")      0
3:如果第一个串中的每个字符都满足式子中的谓词。那么就返回第一个串的长度。

CL-USER> (string< "lisp" "lisper"):     4
4:子串进行比较的时候,如果返回数值的话,仍是相对于原串中的索引。

CL-USER> (string< "family" "amy" :start1 1)  3


Trigonometric Function

The constant pi is a floating-point representation of IT. Its precision is implementation dependent. The functions sin, cos, and tan find the sine,cosine, and tangent, respectively, of angles expressed in radians:

CL> (let ((x (/ pi 4)))
      (list (sin x) (cos x) (tan x)))
(0.7071067811865475d0 0.7071067811865476d0 0.9999999999999999d0)
CL> (sin 1)
0.84147096
CL> (sin pi)
1.2246063538223773d-16
CL> (sin (/ pi 2))
1.0d0

These functions all take negative and complex arguments.

The functions asin, acos, and atan implement the inverse of sine,cosine, and tangent. For arguments between —1 and 1 inclusive, asin and acos return real numbers.

Hyperbolic sine, cosine, and tangent are implemented by sinh, cosh, and tanh, respectively. Their inverses are likewise asinh, acosh, and atanh.

Representation
Common Lisp imposes no limit on the size of integers. Small integers fit in one word of memory and are called JbcnumsWhen a computation produces an integer too large to fit in one memory word, Lisp switches to a representation (a bignum) that uses multiple words of memory.

CL> (values most-positive-fixnum most-negative-fixnum)
536870911
-536870912

Common Lisp provides for up to four types of floating-point numbers: short-float, single-float, double-float, and long-float.

Their names are of the form m-s-f, where m is most or least,s is positive or negative, and f is one of the four types of float

CL> (* most-positive-short-float)
3.4028235e38
CL> (* most-positive-single-float)
3.4028235e38
CL> (* most-positive-double-float)
1.7976931348623157d308
CL> (* most-positive-long-float)
1.7976931348623157d308

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值