17.1
#!/usr/bin/lua
local DoubleList={}
function DoubleList.listNew()
return {first=0,last=-1}
end
function DoubleList.pushFirst(list,value)
local first=list.first-1
list[first]=value
list.first=first
end
function DoubleList.pushLast(list,value)
local last=list.last+1
list.last=last
list[last]=value
end
function DoubleList.popFirst(list)
local first=list.first
if first>list.last then
error("list is empty")
end
local value=list[first]
list[first]=nil
list.first=first+1
return value
end
function DoubleList.popLast(list)
local last=list.last
if list.first>last then
error("list is empty")
end
local value=list[last]
list[last]=nil
list.last=last-1
return value
end
return DoubleList
17.2
local function disk(cx,cy,cr)
return function (x,y)
return (x-cx)^2+(y-cy)^2<=cr^2
end
end
local function rect(left,right,bottom,up)
return function (x,y)
return left<=x and x<=right and bottom<=y and y<=up
end
end
local function complement(r)
return function(x,y)
return not r(x,y)
end
end
local function union(r1,r2)
return function(x,y)
return r1(x,y) or r2(x,y)
end
end
local function intersection(r1,r2)
return function(x,y)
return r1(x,y) and r2(x,y)
end
end
local function difference(r1,r2)
return function(x,y)
return r1(x,y) and not r2(x,y)
end
end
local function translate(r,dx,dy)
return function (x,y)
return r(x-dx,y-dy)
end
end
local function plot(r,M,N)
io.write("P1\n",M," ",N,"\n")
for i=1,N do
local y=(N-i*2)/N
for j=1,M do
local x=(j*2-M)/M
io.write(r(x,y) and "1" or "0")
end
io.write("\n")
end
end
return {
disk=disk,
rect=rect,
complement=complement,
union=union,
intersection=intersection,
difference=difference,
translate=translate,
plot=plot,
}
17.3
路径模板中的问号是用来替换模块名的,如果是固定路径组成,那么这个固定路径所指的文件存在的话,模块必定可以找到,但是这并不一定是希望的那个模块。
17.4
待完善