型別¶
複雜型別¶
自訂型別¶
可以自訂 product type 或 sum type。為求簡化,不支援型別當參數。
-
(Type TYPE-NAME (Type-constronctor x1 x2 ...))
-
(Type TYPE-NAME (U (Type-const1 x1 x2 ...)(Type-const2 x1 x2 ...)...))
-
(Type TYPE-NAME (Type-constronctor))
-
(Type TYPE-NAME (U (Type-const1)(Type-const2)...))
U 是 union 的意思,指 Sum Type。
例如:
(Type IntPair (IntPair Int Int))
如果可以模式匹配,就變這樣:
(\ ((IntPair x))
(match (x)
((IntPair 7 9) 9)
((IntPair 8 a) a)
((IntPair x y) (+ x y))))
Sum Type 的用法:
(Type OrigColor (U (Red)(Green)(Blue))) ; 三原色
如果可以模式匹配,就變這樣:
(orig-color-to-str ((OrigColor x))
(match (x)
((Red) "Red")
((Green) "Green")
((Blue) "Blue")))