Skip to content
许兴逸 edited this page Feb 8, 2024 · 9 revisions

语法

true

bool类型的值。

false

bool类型的值。

nil

nil类型的值。

控制流

if

(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

对任意多个条件进行判断,语法:

(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

(foreach <var> <list>
    <op1>
    <op2>
    <op3>...)

遍历列表并执行一系列操作,例如:

(action main
    (foreach i (int-range 1 10)
        (display i)))

catch

(catch <err-handler>
    <op1>
    <op2>...)

执行一系列操作,当发生错误时,将会把错误字符串作为参数传递给err-handler进行处理。

例如:

(action main
    (define (handler err) (display err))
    (catch handler
        (error "Error!")))

rec

(rec <function>)

用于定义递归函数,将会把传入的函数自身包裹上一层函数传递给<function>

例如:

(define map (rec (lambda (self) (lambda (f ls)
    (if (empty? ls)
        (list)
        (cons (f (car ls)) ((self) f (cdr ls))))))))

表达式

do

(do
    <op1>
    <op2>
    <op3>...)

表示在一个表达式中执行多个操作,最后一个操作的返回值将会被作为该表达式的返回值。

例如:

(task (demo src)
    (in (do
        (display "input: " src)
        src))
    ;; 一些其他的定义...
)

lambda

定义匿名函数。

(lambda (<arg1> <arg2> ...)
    <op1>
    <op2>
    <op3>...)

例子:

(define add (lambda (x y) (+ x y)))
(define clear (lambda () (delete "./build")))

变量

define

参见顶部定义 define

defines

参见顶部定义 defines

Clone this wiki locally