TLS Chapter #10 Examples in Oz
% Defined in previous chapters
fun {IsZero X} X == 0 end
fun {Add1 N} N + 1 end
fun {Sub1 N} N - 1 end
fun {IsAtomS X}
if {IsAtom X} andthen {Not X == nil}
then true
else {IsNumber X}
end
end
fun {First P} P.1 end
fun {Second P} P.2.1 end
fun {Third P} P.2.2.1 end
fun {Build S1 S2} S1|S2|nil end
%%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 10 %%%%%%%%%%%%%%%%%%%%%%
% 10.7
fun {LookupInEntry Name Entry EntryF}
{LookupInEntryHelp Name {First Entry} {Second Entry} EntryF}
end
fun {LookupInEntryHelp Name Names Values EntryF}
case Names
of nil then {EntryF Name}
[] H|T then
if H == Name
then Values.1
else {LookupInEntryHelp Name T Values.2 EntryF}
end
end
end
% 10.2
NewEntry = Build
% 10.1
local A B C in
A = [[appetizer entrée beverage] [paté boeuf vin]]
B = [[appetizer entrée beverage] [beer beer beer]]
C = [[beverage dessert] [[food is] [number one with us]]]
end
% 10.3
local Name Entry in
Name = entrée
Entry = [[appetizer entrée beverage] [food tastes good]]
{Browse 3#{LookupInEntry Name Entry fun {$ _} empty end}}
end
% 10.4
local Name Entry in
Name = dessert
Entry = [[appetizer entrée beverage] [food tastes good]]
{Browse 4#{LookupInEntry Name Entry fun {$ _} empty end}}
end
% 10.8
_ = [[appetizer entrée beverage] [paté boeuf vin]]
_ = [[beverage dessert] [[food is] [number one with us]]]
% 10.9
ExtendTable = fun {$ X Y} X|Y end
% 10.11
fun {LookupInTable Name Table TableF}
case Table
of nil then {TableF Name}
[] H|T then {LookupInEntry Name H fun {$ Name} {LookupInTable Name T TableF} end}
end
end
% 10.10
local Name Entry TableF in
Name = entrée
Entry = [[[entrée dessert] [spaghetti spumoni]]
[[appetizer entrée beverage] [food tastes good]]]
{Browse 10#{LookupInTable Name Entry fun {$ Name} empty end}}
end
% Note: The remainder of the chapter is rough going in Oz as
% TLS dives into a mini Scheme type interpreter.
% 10.18
{Browse 18#[car [qoute [a b c]]]}
% 10.19
local RepA RepB RepC in
RepA = a
RepB = b
RepC = c
{Browse 19#(RepA|RepB|RepC|nil)}
end
% 10.20
local RepCar RepQuote RepA RepB RepC in
RepCar = car
RepQuote = quote
RepA = a
RepB = b
RepC = c
{Browse 20#(RepCar|(RepQuote|(RepA|RepB|RepC|nil)|nil)|nil)}
end
% 10.21
{Browse 21#[car [qoute [a b c]]]}
% 10.22
local E in
E = [car [qoute [a b c]]]
{Browse 22#E}
end
% 10.24
local E in
E = {Add1 6}
{Browse 24#E}
end
% 10.25
local E in
E = 6
{Browse 25#E}
end
% 10.26
local E in
E = nothing
{Browse 26#[quote E]}
end
% 10.28
local E in
E = {fun {$ Nothing} Nothing|nil end
['from' nothing comes something]}
{Browse 28#E}
end
% 10.29
local E in
E = {fun {$ Nothing}
if Nothing
then something
else nothing
end
end
true}
{Browse 29#E}
end
fun {TypeOf X}
case X
of quote|_ then quote
[] lambda|_ then lambda
[] (lambda|_)|_ then application
[] 'cond'|_ then 'cond'
[] true then const
[] false then const
[] quote then quote
[] cons then const
[] car then const
[] cdr then const
else
if {IsNumber X}
then const
else identifier
end
end
end
% 10.30
local E in
E = 6
{Browse 30#{TypeOf E}}
end
% 10.31
local E in
E = false
{Browse 31#{TypeOf E}}
end
% 10.32
local E in
E = false
{Browse 32#E}
end
% 10.33
local E in
E = cons
{Browse 33#{TypeOf E}}
end
% 10.34
local E in
E = car
{Browse 34#E}
end
% 10.35
local E in
E = [quote nothing]
{Browse 35#{TypeOf E}}
end
% 10.36
local E in
{Browse 36#{TypeOf e}}
end
% 10.37
local E in
E = [lambda [x y] [cons x y]]
{Browse 37#{TypeOf E}}
end
% 10.38
local E in
E = [[lambda [nothing]
['cond'
[nothing [quote something]]
['else' [quote nothing]]]]
true]
{Browse 38#{TypeOf E}}
end
% 10.39
local E in
E = ['cond'
[nothing [quote something]]
['else' [quote nothing]]]
{Browse 39#{TypeOf E}}
end
% 10.44
fun {ExpressionToAction E}
if {IsAtomS E}
then {AtomToAction E}
else {ListToAction E}
end
end
fun {AtomToAction E}
case E
of true then Const
[] false then Const
[] cons then Const
[] car then Const
[] cdr then Const
[] isnull then Const
[] iseq then Const
[] isatom then Const
[] iszero then Const
[] add1 then Const
[] sub1 then Const
[] isnumber then Const
else
if {IsNumber E}
then Const
else Identifier
end
end
end
% 10.45
fun {ListToAction E}
case E
of quote|T then Quote
[] lambda|T then Lambda
[] 'cond'|T then Cond
else ApplicationS
end
end
% 10.46
fun {Value E}
{Meaning E nil}
end
fun {Meaning E Table}
{{ExpressionToAction E} E Table}
end
% 10.49
fun {Const E Table}
if {IsNumber E} then E
elseif E == true then true
elseif E == false then false
else [quote primitive]|E|nil
end
end
% 10.50
fun {Quote E Table}
{TextOf E}
end
TextOf = Second
% 10.53
fun {Identifier E Table}
{LookupInTable E Table InitialTable}
end
% 10.54
fun {InitialTable Name}
fail
end
% 10.55
_ = fun {$ X} X end
% 10.60
fun {Lambda E Table}
nonprimitive|(Table|E.2|nil)|nil
end
% 10.61
TableOf = First
FormalsOf = Second
BodyOf = Third
% 10.63
fun {EvCon Lines Table}
if {Else {QuestionOf Lines.1}} then
{Meaning {AnswerOf Lines.1} Table}
elseif {Meaning {QuestionOf Lines.1} Table} == 'true' then
{Meaning {AnswerOf Lines.1} Table}
else
{EvCon Lines.2 Table}
end
end
fun {Else X}
if {IsAtomS X}
then X == 'else'
else false
end
end
QuestionOf = First
AnswerOf = Second
% 10.65
fun {Cond E Table}
{EvCon {CondLinesOf E} Table}
end
fun {CondLinesOf L} L.2 end
% 10.68
local E Table in
E = ['cond' [cofee klatsch] ['else' party]]
Table = [[[cofee] ['true']]
[[klatsch party] [5 [6]]]]
{Browse 68#{Cond E Table}}
end
% 10.74
fun {EvLis Args Table}
case Args
of nil then nil
[] H|T then {Meaning H Table} | {EvLis T Table}
end
end
% 10.77
fun {ApplicationS E Table}
{Apply {Meaning {FunctionOf E} Table}
{EvLis {ArgumentsOf E} Table}}
end
% 10.78
FunctionOf = fun {$ L} L.1 end
ArgumentsOf = fun {$ L} L.2 end
% 10.81
fun {IsPrimitive L}
{First L} == [quote primitive]
end
fun {IsNonPrimitive L}
{First L} == [quote nonprimitive]
end
% 10.82
fun {Apply Fun Vals}
if {IsPrimitive Fun} then
{ApplyPrimitive {Second Fun} Vals}
elseif {IsNonPrimitive Fun} then
{ApplyClosure {Second Fun} Vals}
end
end
% 10.83
fun {ApplyPrimitive Name Vals}
case Name
of cons then {First Vals}|{Second Vals}
[] car then {First Vals}.1
[] cdr then {First Vals}.2
[] [quote iseq] then {First Vals} == {Second Vals}
[] [quote isatom] then {IsAtomColon {First Vals}}
[] [quote iszero] then {IsZero {First Vals}}
[] [quote add1] then {Add1 {First Vals}}
[] [quote sub1] then {Sub1 {First Vals}}
[] [quote isnumber] then {IsNumber {First Vals}}
end
end
fun {IsAtomColon X}
if {IsAtomS X} then true
elseif X == nil then false
elseif X.1 == [quote primitive] then true
elseif X.1 == [quote nonprimitive] then true
else false
end
end
% 10.88
fun {ApplyClosure Closure Vals}
{Meaning {BodyOf Closure}
{ExtendTable
{NewEntry
{FormalsOf Closure}
Vals}
{TableOf Closure}}}
end
% 10.89
local Closure Vals XXX YYY ZZZ in
Closure = [[[[u v w]
[1 2 3]]
[[x y z]
[4 5 6]]]
[x y]
[cons z x]]
Vals = [[a b c] [d e f]]
{Browse 89#{ApplyClosure Closure Vals}}
end
% 10.85
local F A B Table Vals in
F = [lambda [x y] [cons x y]]
A = 1
B = [2]
% not sure what the call is here
end
|