about summary refs log tree commit diff stats
path: root/list.lua
blob: 1153c267d5102c6046c3baf9de22d959cf61f299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
--- lam.list

local list = {}
local util = require "util"
local types = require "types"
table.unpack = table.unpack or unpack

list.Null = setmetatable({}, {
		__type = "Null",
		__tostring = function(self) return "()" end,
})

list.isNull =
	function (x)
		return x == list.Null
	end

list.List =
	function (tbl)
		local function tolist (base, items)
			if #items == 0 then return base end
			return tolist (
				types.Cons(table.remove(items), base),
				items
			)
		end
		return tolist(list.Null, tbl)
	end

list.isList =
	function (x)
		if list.isNull(x) then
			return true
		elseif types.isa(x, "Pair") then
			return list.isList(x[2])
		else
			return false
		end
	end

list.fold1 =
	function (fn, seed, lis)
		if list.isNull(lis) then return seed end
		return list.fold1(fn, fn(seed, lis[1]), lis[2])
	end

---
return list