78 lines
1.7 KiB
Text
78 lines
1.7 KiB
Text
Module Using_AmbFunction {
|
|
Enum Solution {First, Any=-1}
|
|
Function Amb(way as Solution, failure) {
|
|
read a
|
|
c1=lambda i=0, a, (&any, &ret) ->{
|
|
any=(array(a,i),)
|
|
ret=any
|
|
i++
|
|
ok=i=len(a)
|
|
if ok then i=0
|
|
=ok
|
|
}
|
|
m=stack.size
|
|
if m=0 then Error "At least two arrays needed"
|
|
c=c1
|
|
while m>0 {
|
|
read a
|
|
c1=lambda c2=c, i=0, a, (&any, &ret) ->{
|
|
any=(array(a,i),)
|
|
ret=(,) : ok=false : anyother=(,)
|
|
ok=c2(&anyother, &ret)
|
|
ret=cons(ret, any)
|
|
if ok then i++
|
|
ok=i=len(a)
|
|
if ok then i=0
|
|
=ok
|
|
}
|
|
c=c1 : m--
|
|
}
|
|
ok=false
|
|
any=(,)
|
|
flush
|
|
while not ok
|
|
ret=(,)
|
|
ok=c(&any, &ret)
|
|
s=stack(ret)
|
|
if not failure(! s) then data ret : if way>0 then ok=true
|
|
End While
|
|
if empty then
|
|
ret=(("",),)
|
|
else
|
|
ret=array([])
|
|
end if
|
|
=ret
|
|
}
|
|
Range=lambda (a, f) ->{
|
|
for i=a to f-1: data i: next
|
|
=array([])
|
|
}
|
|
|
|
Baker=range(1, 5)
|
|
Cooper=range(2, 6)
|
|
Fletcher=range(2, 5)
|
|
Miller=range(1,6)
|
|
Smith=range(1,6)
|
|
|
|
failure=lambda (Baker, Cooper, Fletcher, Miller, Smith)->{
|
|
if Baker=Cooper or Baker=Fletcher or Baker=Miller or Baker=Smith then =true:exit
|
|
if Cooper=Fletcher or Cooper =Miller or Cooper=Smith then =true:exit
|
|
if Fletcher=Miller or Fletcher=Smith or Miller=Smith then =true:exit
|
|
if Miller<Cooper or abs(Cooper-Fletcher)=1 or abs(Smith-Fletcher)=1 then =true:exit
|
|
}
|
|
all=amb(Any, failure, Baker, Cooper, Fletcher, Miller, Smith)
|
|
k=each(all)
|
|
s=("Baker", "Cooper", "Fletcher", "Miller", "Smith")
|
|
while k
|
|
z=array(k)
|
|
zz=each(z, , -2)
|
|
while zz
|
|
? s#val$(zz^)+" ("+array(zz)+"), ";
|
|
end while
|
|
zz=each(z, -1)
|
|
while zz
|
|
? s#val$(zz^)+" ("+array(zz)+") "
|
|
end while
|
|
end while
|
|
}
|
|
Using_AmbFunction
|