Skip to content

Instantly share code, notes, and snippets.

@Glorp
Created December 11, 2023 15:31
Show Gist options
  • Save Glorp/6b00f4e2a1130d7d64f622d6394d0aa8 to your computer and use it in GitHub Desktop.
Save Glorp/6b00f4e2a1130d7d64f622d6394d0aa8 to your computer and use it in GitHub Desktop.
local Cons = {
__tostring = function(cons) return "(" .. tostring(cons.car) .. " . " .. tostring(cons.cdr) .. ")" end
}
local function cons(car, cdr)
local res = { car = car, cdr = cdr }
setmetatable(res, Cons)
return res
end
local function list(a, ...)
if a == nil then return nil end
return cons(a, list(...))
end
local function map(f, l)
if l == nil then return nil
else return cons(f(l.car), map(f, l.cdr)) end
end
return {
cons = cons,
list = list,
map = map
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment