From bbaff0e0c204c2fab216f6501dc8c11b4425b4bc Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 4 Mar 2024 21:01:27 -0600 Subject: Ugghhhh totally not working --- list.lua | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 list.lua (limited to 'list.lua') diff --git a/list.lua b/list.lua new file mode 100644 index 0000000..1153c26 --- /dev/null +++ b/list.lua @@ -0,0 +1,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 -- cgit 1.4.1-21-gabe81