今天將Haskell的一部分列表處理函數抄進了Kernel FP里,堅持所有函數(模板函數)不寫類型,而讓編譯器進行類型推導:
有函數(其中標明“未解決”的2個函數,所推導的類型是錯誤的):
1 module list
2 import sysutils
3
4 {返回列表長度}
5 def length xs =
6 select xs of
7 case list x tail : iadd 1 (length tail)
8 case empty : 0
9 end
10
11 {返回列表的第一個元素}
12 def head xs =
13 select xs of
14 case list x tail : x
15 end
16
17 {返回列表的第二個元素開始的列表}
18 def tail xs =
19 select xs of
20 case list x tail : tail
21 end
22
23 {連接兩個列表}
24 def concat as bs =
25 select as of
26 case list a tail : list a (concat tail bs)
27 case empty : bs
28 end
29
30 {判讀列表是否為空}
31 def isempty xs =
32 select xs of
33 case list x tail : false
34 case empty : true
35 end
36
37 {將列表通過映射函數轉換為另一個列表}
38 def transform mapper xs =
39 select xs of
40 case list x tail : list (mapper x) (transform mapper tail)
41 case empty : empty
42 end
43
44 {將列表反轉}
45 def reverse xs =
46 let
47 def _reverse xs r =
48 select xs of
49 case list x tail : _reverse tail (list x r)
50 case empty : r
51 end
52 in _reverse xs empty
53
54 {為列表插入分隔符}
55 def intersperse spliter xs =
56 select xs of
57 case list x tail : list spliter (list x (intersperse spliter tail))
58 case empty : empty
59 end
60
61 {將“列表的列表”的所有元素連接起來成為一個長的新列表}
62 def flatten xs =
63 select xs of
64 case list x tail : concat x (flatten tail)
65 case empty : empty
66 end
67
68 {將兩個列表組合成一個pair的列表}
69 def pairlist as bs =
70 select as of
71 case list a atail :
72 select bs of
73 case list b btail : list (pair a b) (pairlist atail btail)
74 case empty : empty
75 end
76 case empty : empty
77 end
78
79 {未解決:將列表應用到一個左結合操作符上}
80 def fold init op xs =
81 select xs of
82 case list x tail : fold (op init x) tail
83 case empty : init
84 end
85
86 {判斷列表的所有元素是否符合某個約束}
87 def all constraint xs = fold true and (transform constraint xs)
88
89 {判斷列表的是否存在元素是否符合某個約束}
90 def any constraint xs = fold false or (transform constraint xs)
91
92 {未解決:遞歸無窮列表}
93 def iterate op init = list init (iterate (op init))
94
95 {重復無窮列表}
96 def repeat x = list x (repeat x)
97
98 {循環(huán)無窮列表}
99 def cycle xs = concat xs (cycle xs)
100
101 {取列表前n個元素組成子列表}
102 def take n xs =
103 if (iequ n 0)
104 empty
105 select xs of
106 case list x tail : list x (take (isub n 1) tail)
107 case empty : empty
108 end
109
110 {取列表n個元素以后的字列表}
111 def drop n xs =
112 if (iequ n 0)
113 xs
114 select xs of
115 case list x tail : take (isub n 1) tail
116 case empty : empty
117 end
118
119 {取列表中符合條件的元素組成的新列表}
120 def takeif constraint xs =
121 select xs of
122 case list x tail : if (constraint x) (list x (takeif constraint tail)) (takeif constraint tail)
123 case empty : empty
124 end
125
126 {取列表中不符合條件的元素組成的新列表}
127 def dropif constraint xs =
128 select xs of
129 case list x tail : if (constraint x) (dropif constraint tail) (list x (dropif constraint tail))
130 case empty : empty
131 end
然后編譯器輸出結果:
1 【模塊:list】
2 module list::list
3 import sysutils
4 func all T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> system.bool)) codefrom 11
5 func any T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> system.bool)) codefrom 12
6 func concat T1 :: ((system.list <T1>) -> ((system.list <T1>) -> (system.list <T1>))) codefrom 3
7 func cycle T1 :: ((system.list <T1>) -> (system.list <T1>)) codefrom 15
8 func drop T1 :: (system.int -> ((system.list <T1>) -> (system.list <T1>))) codefrom 17
9 func dropif T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> (system.list <T1>))) codefrom 19
10 func flatten T1 :: ((system.list (system.list <T1>)) -> (system.list <T1>)) codefrom 8
11 func fold T1 T2 T3 :: (<T1> -> ((<T1> -> (<T2> -> <T3>)) -> ((system.list <T2>) -> <T1>))) codefrom 10
12 func head T1 :: ((system.list <T1>) -> <T1>) codefrom 1
13 func intersperse T1 :: (<T1> -> ((system.list <T1>) -> (system.list <T1>))) codefrom 7
14 func isempty T1 :: ((system.list <T1>) -> system.bool) codefrom 4
15 func iterate T1 T2 :: ((<T1> -> <T2>) -> (<T1> -> (system.list <T1>))) codefrom 13
16 func length T1 :: ((system.list <T1>) -> system.int) codefrom 0
17 func pairlist T1 T2 :: ((system.list <T1>) -> ((system.list <T2>) -> (system.list (sysutils.pair <T1> <T2>)))) codefrom 9
18 func repeat T1 :: (<T1> -> (system.list <T1>)) codefrom 14
19 func reverse T1 :: ((system.list <T1>) -> (system.list <T1>)) codefrom 6
20 func tail T1 :: ((system.list <T1>) -> (system.list <T1>)) codefrom 2
21 func take T1 :: (system.int -> ((system.list <T1>) -> (system.list <T1>))) codefrom 16
22 func takeif T1 :: ((<T1> -> system.bool) -> ((system.list <T1>) -> (system.list <T1>))) codefrom 18
23 func transform T1 T2 :: ((<T1> -> <T2>) -> ((system.list <T1>) -> (system.list <T2>))) codefrom 5
24 【模塊:sysutils】
25 module sysutils::sysutils
26 import system
27 type pair T1 T2
28 ctor pair :: <T1> -> <T2> -> type pair T1 T2
29 func and :: (system.bool -> (system.bool -> system.bool)) codefrom 4
30 func if T1 :: (system.bool -> (<T1> -> (<T1> -> <T1>))) codefrom 9
31 func ineg :: (system.int -> system.int) codefrom 11
32 func not :: (system.bool -> system.bool) codefrom 2
33 func or :: (system.bool -> (system.bool -> system.bool)) codefrom 6
34 func pairop T1 T2 T3 :: ((<T1> -> (<T2> -> <T3>)) -> ((sysutils.pair <T1> <T2>) -> <T3>)) codefrom 12
35 func xor :: (system.bool -> (system.bool -> system.bool)) codefrom 8
posted on 2008-10-07 08:10
陳梓瀚(vczh) 閱讀(1279)
評論(0) 編輯 收藏 引用 所屬分類:
腳本技術