about summary refs log tree commit diff stats
path: root/list.lua
diff options
context:
space:
mode:
Diffstat (limited to 'list.lua')
-rw-r--r--list.lua48
1 files changed, 48 insertions, 0 deletions
diff --git a/list.lua b/list.lua new file mode 100644 index 0000000..1153c26 --- /dev/null +++ b/list.lua
@@ -0,0 +1,48 @@
1--- lam.list
2
3local list = {}
4local util = require "util"
5local types = require "types"
6table.unpack = table.unpack or unpack
7
8list.Null = setmetatable({}, {
9 __type = "Null",
10 __tostring = function(self) return "()" end,
11})
12
13list.isNull =
14 function (x)
15 return x == list.Null
16 end
17
18list.List =
19 function (tbl)
20 local function tolist (base, items)
21 if #items == 0 then return base end
22 return tolist (
23 types.Cons(table.remove(items), base),
24 items
25 )
26 end
27 return tolist(list.Null, tbl)
28 end
29
30list.isList =
31 function (x)
32 if list.isNull(x) then
33 return true
34 elseif types.isa(x, "Pair") then
35 return list.isList(x[2])
36 else
37 return false
38 end
39 end
40
41list.fold1 =
42 function (fn, seed, lis)
43 if list.isNull(lis) then return seed end
44 return list.fold1(fn, fn(seed, lis[1]), lis[2])
45 end
46
47---
48return list