随笔-341  评论-2670  文章-0  trackbacks-0
    今天将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 {循环无穷列表}
 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) 阅读(1258) 评论(0)  编辑 收藏 引用 所属分类: 脚本技术

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理