2 ms·
50-100 lines? How horrifying. function mklist(v, ...) if v==nil then return nil end return {value=v, next=mklist(...)} end function listeq(a,b)
by anonymoushn 13y ago
50-100 lines? How horrifying.
function mklist(v, ...)
if v==nil then return nil end
return {value=v, next=mklist(...)}
end
function listeq(a,b)
if a==nil or b==nil then return a==b end
if a.value==b.value then return listeq(a.next, b.next) end
return false
end
function revlist(list, sofar)
if list == nil then return sofar end
local next = list.next
list.next = sofar
return revlist(next, list)
end
function test()
assert(mklist(1,2,3).next.next.value==3)
assert(mklist(1,2,3).next.next.next==nil)
assert(listeq(mklist(1,2,3),mklist(1,2,3)))
assert(not listeq(mklist(1,2,3),mklist(1,2,5)))
assert(not listeq(mklist(1,2,3),mklist(1,2)))
assert(listeq(mklist(1,2,3), revlist(mklist(3,2,1))))
assert(not listeq(mklist(1,2,3), revlist(mklist(1,2,3))))
end
- AnIrishDuck 13y agoIf you program your responses in C, you get more boilerplate. Still shouldn't be conceptually difficult to any programmer worth their salt.