跳轉到內容

圖示程式設計

0% developed
來自華夏公益教科書,開放的書籍,開放的世界

圖示 - 這是什麼?

[編輯 | 編輯原始碼]

圖示是一種現代高階程式語言。它提供了許多用於管理資料、生成器等的實用功能。它不是面向物件的,但是 UN-icon 變體,UNICON,是面向物件的,就像 ObjectIcon (code.google.com/p/objecticon) 一樣。

Hello world

[編輯 | 編輯原始碼]

這是一個很小但完整的圖示程式。

procedure main()
  write("Hello world!")
end

如您所見,程式碼非常簡單。行 procedure main()end 包含程式的主體。第二行負責在您的標準輸出(通常是螢幕)上列印 Hello world!。自然地,過程 write 列印作為引數給出的字串。有一個類似的過程 read 用於從標準輸入(通常是鍵盤)讀取一行。

編譯和執行

[編輯 | 編輯原始碼]

將上一節中的程式儲存到檔案 helloworld.icn 中。鍵入

icont helloworld.icn

在 Linux shell 中。將生成名為 helloworld 的可執行檔案。您現在可以執行它。

基本語法

[編輯 | 編輯原始碼]

程式由過程組成。每個過程都以保留字 procedure 開始,後面跟著過程的名稱和引數列表(可以為空)。過程以 end 結束。所有指令都以換行符或分號分隔。

必須有一個 main 過程,當用戶執行程式時會自動呼叫該過程。

procedure p1(arg1, arg2, arg3)
  instr1
  instr2
end

procedure p2()
  instr1; instr2
end

procedure main(arg)
  instr1
  instr2
  instr3
end

如果您想在程式碼中添加註釋,請使用 # 字元。編譯器將忽略該字元之後的該行中的所有文字。

關於圖示中的變數

[編輯 | 編輯原始碼]

圖示中的變數沒有型別,可以包含任何資料型別的值。這意味著您可以例如將數字分配給其中一個變數,並在幾條指令後將字串分配給它。

無需宣告變數(全域性變數除外),它們在首次使用時建立。也不需要銷燬它們,因為有一個垃圾收集器自動執行此操作。

給變數賦值

[編輯 | 編輯原始碼]

要給變數賦值,請使用 Pascal 類似的 := 運算子

procedure main()
  x := 1                     # assigns 1 to variable x
  y := 3                     # assigns 3 to variable y
  x := x + y                 # assigns 4 (1+3) to variable x
  y := "example string"      # assigns string "example string" to variable y
end

全域性變數

[編輯 | 編輯原始碼]

圖示中字串介紹

[編輯 | 編輯原始碼]

字串是一系列字元。您可以輕鬆地將字串常量放入程式碼中,用一對 " 字元將其括起來。例如

str := "abcdefgh"

將建立一個包含 8 個字元的字串常量,並將此字串分配給 str 變數。

訪問字元和子字串

[編輯 | 編輯原始碼]

字串中的字元以非常方便的方式索引。與 Pascal 一樣,1 指向第一個字元之前的位,2 指向第一個和第二個字元之間的位,依此類推。與許多其他語言不同,字串也可以從末尾索引。0 指向最後一個字元之後的位,-1 指向最後一個字元之前的位,依此類推。

要訪問您想要的字串的一部分,請使用方括號 [ ] 運算子

  • variablename[from:to]
  • variablename[index]

第一種方法返回位於 fromto 指向的點之間的子字串。如果您在方括號中只放置一個數字,它等效於 variablename[index:index+1]

str := "sample string"
str[1:7] := "SAMPLE"    # str := "SAMPLE string"
str[3] := "m"           # str := "SAmPLE string"
write(str[8:0])         # write("string")

特殊字元

[編輯 | 編輯原始碼]

運算子

[編輯 | 編輯原始碼]
  • *str - 生成 str 的長度
  • ?str - 生成 str 的隨機單字元子字串
  • !str - 生成 str 的單字元子字串序列
  • str1 || str2 - 連線兩個字串
str := "Hello" || " " || "world"       # str := "Hello world"
str ||:= "!"                           # str := "Hello world!"
  • 字串區分大小寫比較
    • str1 == str2 - 如果兩個字串相等,則輸出 str2,否則失敗
    • str1 ~== str2 - 如果兩個字串不相等,則輸出 str2,否則失敗
    • str1 << str2 - 如果 str2 大於 str1,則輸出 str2,否則失敗
    • str1 <<= str2 - 如果 str2 大於或等於 str1,則輸出 str2,否則失敗
    • str1 >> str2 - 如果 str2 小於 str1,則輸出 str2,否則失敗
    • str1 >>= str2 - 如果 str2 小於或等於 str1,則輸出 str2,否則失敗
  • str ? expr - 將 str 設定為 expr 表示式的 &subject

字串掃描

[編輯 | 編輯原始碼]

列表概述

[編輯 | 編輯原始碼]

列表類似於字串,但它們可以包含更多不同的資料。像字串一樣,列表從開頭和結尾索引(請閱讀與字串相關的訪問字元和子字串部分)。

列表建立

[編輯 | 編輯原始碼]

您可以使用方括號將列表常量的元素括起來,並用,字元分隔它們來建立列表常量。例如

lst := [1, "hello", 44]

建立了三個元素的列表常量,並將其分配給 lst 變數。要建立空列表,不要在方括號之間放置任何內容

lst := []                     # lst value is now an empty list

有一個用於建立所需長度的列表的特殊函式

  • list (i,x)

建立 i 元素列表,所有元素都初始化為 x 值。預設情況下,i 為 0,x&null

list(5, 7)            # [7, 7, 7, 7, 7]
list(3, "text")       # ["text", "text", "text"]
list(6)               # [&null, &null, &null, &null, &null, &null]
list()                # []

運算子

[編輯 | 編輯原始碼]

Icon 中沒有陣列。使用列表代替。如果您需要多維陣列,請使用列表的列表。

matrix := list(3)                # makes a list of 3 rows
every !matrix := list(5, 0)      # each a different list of 5 zeroes
matrix[2][4] +:= 5               # adds 5 to the element
                                 # in 2nd row and 4th column 
every write(!!matrix)            # writes all the elements

堆疊和佇列

[編輯 | 編輯原始碼]

提供了一些非常有用的函式來管理堆疊和佇列作為列表。

  • push(list, elem) - 將 elem 新增到 list 列表的開頭。
  • pop(list) - 輸出 list 的第一個元素,並將其從 list 中刪除,或者如果 list 為空則失敗。
  • put(list, elem) - 將 elem 新增到 list 的末尾。
  • get(list) - 輸出 list 的第一個元素,並將其從 list 中刪除,或者如果 list 為空則失敗,它與 pop 相同。
  • pull(list) - 輸出 list 的最後一個元素,並將其從 list 中刪除,或者如果 list 為空則失敗。
queue := []                      # creates an empty queue
put(queue, 1)                    # puts 1 into the queue
put(queue, "it is a string")     # puts "it is a string" into the queue
write(get(queue))                # writes 1
write(get(queue))                # writes "it is a string"
                                 # queue is now empty
stack := []                      # creates an empty stack
put(stack, 1)                    # puts 1 into the stack
put(stack, "it is a string")     # puts "it is a string" into the stack
write(get(stack))                # writes "it is a string"
write(get(stack))                # writes 1
                                 # stack is now empty
write(*queue)                    # writes length of the queue

字元集

[編輯 | 編輯原始碼]

記錄型別

[編輯 | 編輯原始碼]

記錄型別宣告

[編輯 | 編輯原始碼]

要宣告記錄型別,請使用 record 保留字,例如

record person(firstname, lastname, age)

記錄變數的構造

[編輯 | 編輯原始碼]

當您宣告一個記錄時,會自動定義一個建構函式。建構函式是一個過程,其引數是記錄欄位的初始值,並返回建立的記錄。建構函式的名稱與記錄的名稱相同。

record person(firstname, lastname, age)      # declaration of person record
jd := person("John", "Doe", 47)              # create and assign record value

運算子

[編輯 | 編輯原始碼]
  • rec.fieldname - 輸出 rec 記錄的 fieldname 欄位的值
  • *rec - 輸出 rec 記錄的欄位數量
  • ?rec - 輸出 rec 記錄的隨機欄位的值
  • !rec - 生成 rec 記錄的所有欄位的值

作為 Icon 語言一部分的過程通常稱為“函式”。使用者作為程式的一部分新增的函式稱為“過程”。過程可以透過在識別符號前加上保留字“return”來返回一個值。

#example of the use of the reserved word return
procedure ex-ret(var1, _Var2)
b := "a_proc succeeded"
if a_proc(var1) then return b else c_proc(_Var2)
end

生成器

[編輯 | 編輯原始碼]

生成器是新語言(如 Ruby 和 Python)中非常基礎的一部分,最初是在 CLU 之後不久就在 ICON 中使用。

目標導向執行

[編輯 | 編輯原始碼]

許多 ICON 表示式稱為“條件表示式”,它們的評估結果是成功或失敗。三個重要的例子是

 read(res) 
 find( Str_target, Str_source)  
 match( Str_target, Str_source)

比較運算子形成成功或失敗的表示式,例如

 write(count > 0)

如果 count 不大於零,則條件表示式中的比較失敗;如果沒有表示式,則封閉的表示式(即對函式 write() 的呼叫)永遠不會被評估,在這種意義上,它繼承了條件表示式的失敗。相比之下

 b := "paused"
 if count > 0 then b := "running"
 write(b)  # will write "paused"

目標導向執行不是 Icon 的基礎,就像在 Prolog 中一樣。Icon 更像是 Oz,其中回溯是一個選項。在 Icon 中,回溯是可用的,但在特定上下文的限制內。ICON 也有“資料回溯”的概念

[編輯 | 編輯原始碼]
華夏公益教科書