基本函數介紹

四則運算

+ : (-> int int int)

整數相加。

- : (-> int int int)

整數相減。

* : (-> int int int)

整數相乘。

/ : (-> int int int)

整數相除,得商數。除以 0 的時候跳出特例,中止程式。如果用 C 語言表示的話:

static jmp_buf buf;
...
void int_div_closure_thunk(Object* closure, Object* lhs, Object* rhs, Object* result){
  if (lhs->value==0){
      printf("Exception: Div 0 Error.");
      longjmp(buf,1);
  }

}
...
int main(void){
  int jmpVal = setjmp(buf);
  if (jmpVal == 0){
  // main_body
  }else{
  // exit the program with the exception
  printf("Exit the program with the exception");
  }
}
+. : (-> dou dou dou)

浮點數相加。

-. : (-> dou dou dou)

浮點數相減。

*. : (-> dou dou dou)

浮點數相乘。

/. : (-> dou dou dou)

浮點數相除。

布爾值

and : (-> bool bool bool)

AND。

or : (-> bool bool bool)

OR。

not : (-> bool bool)

NOT。

流程控制

(if cond then alt)

若 cond 為真則返回 then,否則返回 alt。

影印字元

print-str : (-> str void)

列印字串。void 表示不返回任何東西。

int-to-str : (-> int str)

整數轉成十進位字串(10 為 base)。

dou-to-str : (-> int str)

浮點數轉成十進位字串(10 為 base)。

定義變數 (=)

非函數

語法如下:

(def Type var expression)

例如:

(def int a 10)
(def dou b (+ 3.1 7.0))

..note

因為 tshunhue 裡面,所有變數都是不可變物件, ``define`` 不可以拿來重新設定值。定義閉包也一樣。

函數(閉包)

閉包的定義比較複雜。但大致如下:

(def Type var lambda-function)

例如:

(def (-> int int) int-add1 (\ ((int x)) (+ x 1)))
(def (-> dou dou dou) sqrt-sum (\ ((dou x)(dou y)) (+. (*. x x) (*. y y))))

匿名函數 (lambda function)

定義:

(\ () body+)
(\ ((Type var)) body+)
(\ ((Type1 var1)(Type2 var2)[...]*) body+)

所有函數都是第一類物件與閉包,用 lexical scope 保存前文變數。