常量、变量
常量是它的值等于自身的表达式,如123,”abc”…
变量是保存了其它值的表达式,如 x=3, x 即变量。
算术与逻辑操作符
函数名
语法
对应操作符
Data Access
arrayref
a[index]
[ ]
setarray
a[index] = expr
bitfield1
x
<>
setqbitfield1
x=expr
setqbitfield
x=expr
quote
getqq
getq
'expr
g.s
g->s
'
.
->
putpropqq
g.s=expr, g->s=expr
putpropq
d~>s, d~>s =expr ~>
Unary
preincrement
++s
postincrement
s++
predecrement
--s
postdecrement
s--
minus
not
-n
!expr
++
++
--
--
-
!
bnot
expt
times
~x
Binary
n1 ** n2
n1 * n2
quotient
n1 / n2
plus
n1 + n2
difference
n1 - n2
leftshift
x1 << x2
rightshift
x1 >> x2
lessp
n1n2
leqp
geqp
equal
nequal
band
bnand
bxor
bxnor
bor
bnor
and
~
**
*
/
+
-
<<
>>
<
>
<=
>=
==
!=
&
n1<=n2
n1>=n2
g1 == g2
g1 != g2
x1 & x2
x1 ~& x2
~&
x1 ^ x2
x1 ~^ x2
x1 | x2
x1 ~| x2
^
~^
|
~|
x1 && x2
&&
or
range
setq
x1 || x2
g1 : g2
s = expr
||
:
=
对于既有函数名又有操作符函数,采用函数名调用和采用操作符调用的效果是一样的。
示例 lessp(3 1)=>nil 等同于 3<1=>nil
预定义算术函数
语法
结果
通用函数
n + 1
n – 1
绝对值
自然对数的 n 次方
add1(n)
sub1(n)
abs(n)
exp(n)
log(n)
max(n1 n2 ...)
min(n1 n2 ...)
mod(x1 x2)
取模运算
round(n)
sqrt(n)
sxtd(x w)
将 x 右扩展 W 个符号位
zxtd(x w)
和上面的功能相同,速度更快
三角函数
sin(n),cos(n),tan(n)
asin(n),acos(n),atan(n)
随机数生成函数
random(x)
产生0~x-1的随机数
srandom(x)
初始化随机数生成函数的 x
位操作
含义
操作符
位与
位或
位异或
左移位
右移位
取反
&
|
^
>>
<<
~
真值、假值
真值,除了 nil 以为的值都是真值。
假值,nil
条件控制函数
if, when, unless, cond
if
if 函数的语法结构
if( condition1 then
;当 condition1为真值时执行代码块 expA
expA
else
if( condition2 then ;当 condition2为真值时执行代码块 expB
else
expB
…
expC
)
);end if
when
when 函数语法结构
when( condition
;当 condition 为真值时执行代码块 expA,否则跳过
expA
); end when
unless
unless 语法结构
unless( condition
;当 condition 为假值时执行代码块 expA,否则跳过
expA
); end unless
cond
cond 语法结构
cond (
( condition1 expA... ) ;当 condition1为真值时执行代码块 expA
( condition2 expB ... ) ;…
( condition3 expC ... ) ;…
( t expN )
;之前的条件都不满足时默认执行 expN
); end cond
[注]:";" 表示注释
循环控制函数
while,for,foreach
while
while 语法结构
while( condition is true ;当 condition 为真值时反复执行代码块 expA,否则
结束循环
)
expA
for
for 语法结构
for( i 1 5
println(i)
)
将输出
1
2
3
4
5
foreach
foreach 是简化版的 for,上面的例子用 foreach 写如下
alist = ‘(1 2 3 4 5)
foreach( item alist
println( item )
)
foreach 通常用于操作列表,因为它会遍历列表的每一个元素。
选择函数
caseq 的用法类似 cond,功能也类似。
caseq( var
(condition1 expA)
(condition2 expB)
…
) ;case 语法结构
循环结构中的局部变量
使用关键字 prog 可以在循环结构中定义局部变量。关于 prog 的详细介绍请参考函数章节。
返回函数
表示返回值的关键字是 return。return 可以强制一个 prog 结构立即结束,送出返回值,
忽略剩余的循环操作。
示例 prog( ( )
for( i 0 10
when( oddp( i )
return( i )
) ; when
) ; for
) ; prog
这个 for 循环不会运行10次,首次 oddp(i)为真值的时候循环就会结束。
正则表达式