資料結構 scheme 復合型別

2021-06-22 13:43:50 字數 2135 閱讀 5539

; composite types

; (sometimes also referred to as plain old data structures.)

; 陣列或向量,固定長度的序列 ,array

(define v (vector 2 3 "good" #t))

(define x (make-vector 10)) ; x 為10個0

(+ (vector-ref v 1) (vector-ref x 3)) ; 3

(vector-set! x 3 12) ; x = #(0 0 0 12 0 0 0 0 0 0)

; 鍊錶,動態長度的序列 , list

(define res (list 2 3 "new" #t)) ; (2 3 "new" #t)

(+ 1 (list-ref res 1)) ; 4

(car res) ; 2

(cdr res) ; (3 "new" #t)

(null? res) ; 判斷空鍊錶 #f

; 二元點對, 鍊錶是點對的擴充套件 , pair

(define p (cons "age" 18)) ; ("age" . 18)

(car p) ; "age"

(cdr p) ; 18

(set-car! p "years") ; p = ("years" . 18)

(set-cdr! p 20) ; p = ("years" . 20)

(cons 2 (cons 3 (cons "new" (cons #t (list))))) ; (list 2 3 "new" #t)

; 雜湊表,固定時間查詢表 , hashtable

(define ht (make-eq-hashtable))

(hashtable-set! ht 'go 20)

(hashtable-ref ht 'go "not found") ; 20

(hashtable-contains? ht 'go) ; #t

(hashtable-delete! ht 'go)

; 記錄或結構體,record (also called tuple or struct)

(define-record-type

point (fields ; 定義二維點型別

(mutable x) ; x 座標可修改

y)) ; y 座標唯讀

(define p (make-point 36 -17))

(point? p) ; #t

(point? '(cons 36 -17)) ; #f

(point-x p) ; 36

(point-y p) ; -17

(point-x-set! p (- (point-x p) 12))

(point-x p) ; 24

; 聯合體,同一欄位可存放不同型別的資料,union

(point-x-set! p "age") ; tagged union

(point-x-set! p #t) ; (also called a variant, variant record, discriminated union, or disjoint union)

復合型別資料

1 記錄型別 自定義record型別 declare type example record type is record name emp.ename type,salary emp.salary type,dno emp.deptno type 宣告record型別變數 example reco...

Hive復合資料結構簡介

一般情況下,數倉儲存的資料型別都是整型 bigint 字串型 string 浮點型 double 但是有些時候也需要用到一些復合資料結構來儲存資料,常用的復合資料結構有map,array,struct,本文將主要針對這三種資料型別做乙個介紹 型別定義方法 描述array array data typ...

Scala中結構型別 復合型別解析

scala具有豐富的資料結構型別,結構型別 定義乙個方法,該方法的入參是乙個匿名型別物件,該類包含若干個函式或方法,這樣就可以定義乙個具有結構型別的方法 當呼叫該方法時傳入具有具體實現方法的結構型別物件 注 1 函式的入參型別是乙個匿名型別 我們自己可以定義 2 通過type型別定義匿名型別例項物件...