-
Notifications
You must be signed in to change notification settings - Fork 0
许兴逸 edited this page Feb 8, 2024
·
9 revisions
bool
类型的值。
bool
类型的值。
nil
类型的值。
(if <condition> <if-true> [if-false])
当<condition>
为真时,返回<if-true>
,否则返回[if-false]
。
例子:
(action main
(define animal "rabbit")
(define is-rabbit
(if (eq? animal "rabbit")
(do
(display "animal is rabbit!")
true)
(do
(display "animal is not rabbit!")
false)))
(display is-rabbit))
对任意多个条件进行判断,语法:
(cond
(<condition1> <operation1>)
(<condition2> <operation2>)
(<condition3> <operation3>)...)
cond
将会从上到下判断每一种情况,返回第一个<condition>
为真的<operation>
。
通常,最后一个<condition>
为true
以匹配所有可能性。
例:
(action main
(define animal "rabbit")
(display
(cond
((eq? animal "rabbit") 1)
((eq? animal "cat") 2)
((eq? animal "dog") 3)
(true 0))))
(foreach <var> <list>
<op1>
<op2>
<op3>...)
遍历列表并执行一系列操作,例如:
(action main
(foreach i (int-range 1 10)
(display i)))
(catch <err-handler>
<op1>
<op2>...)
执行一系列操作,当发生错误时,将会把错误字符串作为参数传递给err-handler
进行处理。
例如:
(action main
(define (handler err) (display err))
(catch handler
(error "Error!")))
(rec <function>)
用于定义递归函数,将会把传入的函数自身包裹上一层函数传递给<function>
。
例如:
(define map (rec (lambda (self) (lambda (f ls)
(if (empty? ls)
(list)
(cons (f (car ls)) ((self) f (cdr ls))))))))
(do
<op1>
<op2>
<op3>...)
表示在一个表达式中执行多个操作,最后一个操作的返回值将会被作为该表达式的返回值。
例如:
(task (demo src)
(in (do
(display "input: " src)
src))
;; 一些其他的定义...
)
定义匿名函数。
(lambda (<arg1> <arg2> ...)
<op1>
<op2>
<op3>...)
例子:
(define add (lambda (x y) (+ x y)))
(define clear (lambda () (delete "./build")))
参见顶部定义 define。
参见顶部定义 defines。
弦语蝶梦独立游戏工作室