REBOL_基础
============ 1 数据类型 ===========
数据类型 示例
integer 1234
decimal 12.34
string "REBOL world!"
time 15:47:02
date 12-December-2002
tuple 192.168.0.16
money EUR$0.79
pair 640x480
char #"R"
binary #{ab82408b}
email vpavlu@plain.at
issue #ISBN-020-1485-41-9
tag <img src="cover.png">
file %/c/rebol/rebol.exe
url http://plain.at/vpavlu/
block [good bad ugly]
integer 1234
decimal 12.34
string "REBOL world!"
time 15:47:02
date 12-December-2002
tuple 192.168.0.16
money EUR$0.79
pair 640x480
char #"R"
binary #{ab82408b}
email vpavlu@plain.at
issue #ISBN-020-1485-41-9
tag <img src="cover.png">
file %/c/rebol/rebol.exe
url http://plain.at/vpavlu/
block [good bad ugly]
各种类型之间的转换,可以使用 to-****等,具体情况可在命令行中打以下命令查询:
>> ? to-
>> ? to-
============ 2 词 ===========
***在REBOL中,就连 if 这样的C语言中的关键字都可以被赋值(当然这样在以后使用时会出问题):
>> num: 12
== 12
>> if: "some string"
== "some string"
== 12
>> if: "some string"
== "some string"
***任何词可以被赋予任何值,而当一个词没有被赋予值时,就会出错!
>> print foobar
** Script Error: foobar has no value
** Near: print foobar
>> print foobar
** Script Error: foobar has no value
** Near: print foobar
***词的类型:
类型 示例 用途
word var 求值
get-word :var 取 词中的值
set-word var: 置 词中的值
lit-word 'var 词名原义
举例
>> func1: now ; 词 func1 被置为当前时间
== 12-Dec-2002/15:21:15+1:00 ; func1的值被打印
>> func2: :now ; 词 func2 被置为 [ 取出 词 now 中的代码], 即,现在 func2 等同于 now 这个命令
>> wait 0:01 ;1 minute ; 等待1分钟
>> func1 ; 求值 func1 ,还是1分钟以前所存入的值
== 12-Dec-2002/15:21:15+1:00
>> func2 ; 求值 func2,现在的时间,相当于调用了 now 命令
== 12-Dec-2002/15:22:15+1:00
***一个词被赋值的同时,会返回这个值,所以:
>> print a: "REBOL"
REBOL
*** 词--原义 的使用
>> dump: func [ word ][
either value? word [
print [ word "is" get word ] ;当 参数 word 内有值时,取出这个值并打印
][
print [ word "is undefined" ] ;否则 说明 这个词未被定义
]
]
>> a: 42
== 42
>> dump 'a ;注意 'a 是把本身的词名传入dump函数,如果为 a 则是把值传入函数
a is 42
>> dump 'b
b is undefined
>> dump: func [ word ][
either value? word [
print [ word "is" get word ] ;当 参数 word 内有值时,取出这个值并打印
][
print [ word "is undefined" ] ;否则 说明 这个词未被定义
]
]
>> a: 42
== 42
>> dump 'a ;注意 'a 是把本身的词名传入dump函数,如果为 a 则是把值传入函数
a is 42
>> dump 'b
b is undefined
*** 另一种取/赋值方法
>> set 'name "REBOL" ;相当于 name: "REBOL"
>> get 'name ;相当于 :name
>> set 'name "REBOL" ;相当于 name: "REBOL"
>> get 'name ;相当于 :name
*** 取消一个词的值, 可用 unset ,例:
>> word: $100
== $100.00
>> print word
$100.00
>> value? 'word
== true ;此时word有值
>> unset 'word ;取消word内的值
>> value? 'word
== false ;此时无值
>> print word ;打印这个词,就会出错!
** Script Error: word has no value
** Near: print word
>> word: $100
== $100.00
>> print word
$100.00
>> value? 'word
== true ;此时word有值
>> unset 'word ;取消word内的值
>> value? 'word
== false ;此时无值
>> print word ;打印这个词,就会出错!
** Script Error: word has no value
** Near: print word
*** 保护一个词 ,可用protect , 然后可以用unprotect 来解除对它的保护
>> chr: #"R"
== #"R"
>> protect 'chr ;保护 chr 这个词,再对它赋值就会出错
>> chr: #"A"
** Script Error: Word chr is protected, cannot modify
** Near: chr: #"A"
>> unprotect 'chr ;解除对 chr 的保护
>> chr: #"A" ;又可以被赋值了
== #"A"
== #"R"
>> protect 'chr ;保护 chr 这个词,再对它赋值就会出错
>> chr: #"A"
** Script Error: Word chr is protected, cannot modify
** Near: chr: #"A"
>> unprotect 'chr ;解除对 chr 的保护
>> chr: #"A" ;又可以被赋值了
== #"A"
============ 3 块 ===========
值和词都是在块中组合。一个块由一个'['开始,以']'结束。你可以以任何顺序组织值和词,用任意多行代码。REBOL允许在块中以一种自由体来创建数据库、目录、表格、集合、序列、代码、函数,等等。
值和词都是在块中组合。一个块由一个'['开始,以']'结束。你可以以任何顺序组织值和词,用任意多行代码。REBOL允许在块中以一种自由体来创建数据库、目录、表格、集合、序列、代码、函数,等等。
块可以有任意尺寸、任意深度、并且其内的类型也可以是任意的。
>> colors: [red green blue]
== [red green blue]
>> data: [now/date colors [colors $12] 4]
== [now/date colors [colors $12.00] 4]
== [red green blue]
>> data: [now/date colors [colors $12] 4]
== [now/date colors [colors $12.00] 4]
以上都是一些有效的例子。
第1行的示例,块由3个词组成(注意:这3个词还没有定义,但是这没有什么影响,因为在真正调用这些词以前REBOL并不求值)
>> do [now/date colors [colors $12] 4]
== 4
>> data: reduce [now/date colors [colors $12] 4]
== [12-Dec-2002 [red green blue] [colors $12.00] 4]
**** do 对块求值,并返回块内最后一个词的值 , reduce 也对块求值,但它返回块内每个元素的值并生一个新的块来存放。
>> compose [ now/date (now/date) ]
== [now/date 12-Dec-2002]
== [now/date 12-Dec-2002]
**** compose 也是对块求值,所不同的是它只对块内的、被圆括号所包起来的范围求值。这将用来创建一些同时包含代码和数据的代码。
============ 总结 ===========
这就是REBOL里的3种类型, 值,词,块。它们可以用来构建任何比如:变量、控制结构、函数、数据等等。。因此,在REBOL中,代码和数据没有什么真正意义的不同。
这就是REBOL里的3种类型, 值,词,块。它们可以用来构建任何比如:变量、控制结构、函数、数据等等。。因此,在REBOL中,代码和数据没有什么真正意义的不同。
============ 控制结构 ===========
REBOL C/C++
--------------------------------------
REBOL C/C++
--------------------------------------
do [...] {...}
DO 评估1个块. 或者一个 string, 或者一个 file, …
DO 评估1个块. 或者一个 string, 或者一个 file, …
if expr [...] if(expr) {...}
只有当 表达式为真时,块才能被执行。。
either expr [...][...] if(expr) {...} else {...}
当表达式为真,执行第1块;否则,执行第2块;注意REBOL里没有else.
while [expr][ while(expr){
... ...
] }
While is the only control statement that has its condition inside a block. If more than one
condition is found inside the condition block, all conditions must be met in order to have the
loop executed.
for i 1 10 2 [ for(i=1;i<=10;i+=2){
... ...
] }
... ...
] }
until [ do {
... ...
expr ...
] } while(expr);
Until takes the following block and keeps evaluating it as long as the last word evaluates to
true.
loop 10 [...] // N/A in C++
Repeats the passed block 10 times.
repeat i 10 [...] for(i=1;i<=10;i++) {...}
Increments i from 1 to 10 and evaluates the block for every i.
forever [...] while(1){...}
A loop that never ends. Most times a BREAK is found inside this loop so that it is left again.
BREAK can be used to exit all kinds of loops.
switch/default var [ switch(var){
1 [...] case 1: ... break;
2 [...] case 2: ... break;
][...] default: ...
}
============ 什么是真? ===========
>> if 0 [ print "this is important!" ]
this is important!
**** 除了 flase none 以外的所有都是真;所以,上面的 0 也是条件为真的,这点和C、C++不同。
**** 逻辑比较函数
NOT a 返回a的相反值
a AND b logic: true if both are true, false otherwise
a OR b logic: false if both are false, true otherwise
a XOR b logic: true if exact one is true, false otherwise
a AND b logic: true if both are true, false otherwise
a OR b logic: false if both are false, true otherwise
a XOR b logic: true if exact one is true, false otherwise
all [] none on the first word that evaluates to false, last value otherwise
any [] returns the first value that evaluates to true, none otherwise