about summary refs log tree commit diff stats
path: root/global.lua
diff options
context:
space:
mode:
Diffstat (limited to 'global.lua')
-rw-r--r--global.lua162
1 files changed, 0 insertions, 162 deletions
diff --git a/global.lua b/global.lua deleted file mode 100644 index 1dea773..0000000 --- a/global.lua +++ /dev/null
@@ -1,162 +0,0 @@
1--- lam.environment
2
3local util = require "util"
4local types = require "types"
5table.unpack = table.unpack or unpack
6
7local global = {
8 -- constants ---- TODO this should be at the reader level
9 ["#t"] = true,
10 ["#f"] = false,
11}
12
13--- Types ---
14
15global.luatype = type
16global.type = types.lamtype
17
18global["number?"] = function (x) types.isa(x, "Number") end
19global["string?"] = function (x) types.isa(x, "String") end
20global["symbol?"] = function (x) types.isa(x, "Symbol") end
21global["pair?"] = function (x) types.isa(x, "Pair") end
22global["is-a?"] = function (x, t) types.isa(x, t) end
23
24--- Basic functions ---
25
26global.car = function (pair) return pair[1] end
27global.cdr = function (pair) return pair[2] end
28
29-- global.list = types.List
30
31global["list?"] =
32 function (x)
33 -- TODO : detect circular lists
34 if type(x) == "table" then
35 if #x == 0 then return true end
36 if type(x[2]) ~= "table" then return false end
37 end
38 return global["list?"](x[2])
39 end
40
41global["null?"] = function (x) return type(x) == "table" and #x == 0 end
42
43--- Higher-order functions ---
44--[[
45global.apply = function(fn, ...)
46 local args = {...}
47 local last = args[#args]
48 assert(types.luatype(last) == "table", "Bad apply")
49 table.remove(args)
50 for _, v in ipairs(last) do
51 table.insert(args, v)
52 end
53 return fn(table.unpack(args))
54end
55
56global.map = function(fn, list)
57 return util.map(fn, list)
58end
59--]]
60--- Math ---
61-- NOTE: we do not have the full numeric tower yet!
62
63for name, func in pairs(math) do
64 global[name] = func
65end
66
67global.fold =
68 function (fn, lis)
69 local out = {}
70
71 return types.List(out)
72 end
73
74global["+"] = function (lis)
75 return
76 return util.reduce({...}, 0, function (a, b) return a + b end)
77end
78
79global["-"] = function (...)
80 local args = {...}
81 if #args == 0 then
82 error("Too few arguments: need at least 1")
83 elseif #args == 1 then
84 return (-args[1])
85 else
86 local result = args[1]
87 for v = 2, #args do
88 result = result - args[v]
89 end
90 return result
91 end
92end
93
94global["*"] = function (...)
95 local result = 1
96 for _, v in ipairs({...}) do
97 if v == 0 then return 0 end
98 result = result * v
99 end
100 return result
101end
102
103global["/"] = function (...)
104 local args = {...}
105 if #args == 0 then
106 error("Too few arguments: need at least 1")
107 elseif #args == 1 then
108 if args[1] == 0 then error("Division by zero") end
109 return (1/args[1])
110 else
111 local result = args[1]
112 for v = 2, #args do
113 if args[v] == 0 then error("Division by zero") end
114 result = result / args[v]
115 end
116 return result
117 end
118end
119
120--[[
121global["="] =
122 function (...)
123 for _, v in ipairs({...}) do
124 if not a == b then return false end
125 end
126 return true
127 end
128
129global["<"] =
130 function (...)
131 for _, v in ipairs({...}) do
132 if not a < b then return false end
133 end
134 return true
135 end
136
137global["<="] =
138 function (...)
139 for _, v in ipairs({...}) do
140 if not a <= b then return false end
141 end
142 return true
143end
144
145global[">"] =
146 function (...)
147 for _, v in ipairs({...}) do
148 if not a > b then return false end
149 end
150 return true
151end
152
153global[">="] =
154 function (...)
155 for _, v in ipairs({...}) do
156 if not a >= b then return false end
157 end
158 return true
159end
160--]]
161---
162return global