%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Syntactic sugar examples %
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
local X A B in
A = "George"
B = 25
X = person(name:A age:B)
{Browse X}
end
local X = person(name:"George" age:25) in {Browse X} end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
local T in
local A B C D in
T = tree(key:A left:B right:C value:D)
A = 1
B = 2
C = 3
D = 4
end
{Browse T}
end
local T in
local tree(key:A left:B right:C value:D) = T in
A = 1
B = 2
C = 3
D = 4
end
{Browse T}
end
local A B C D T = tree(key:A left:B right:C value:D) in
A = 1
B = 2
C = 3
D = 4
{Browse T}
end
%Exercise
%Here is another attempt at a construct equivalent to the above.
%Will it work? Why or why not?
%
%local T = tree(key:A left:B right:C value:D) in
% A = 1
% B = 2
% C = 3
% D = 4
% {Browse T}
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
local T in
local tree(key:A left:B right:C value:D)=T in A = 1 B = 2 C = 3 D = 4 end
{Browse T}
end
%Exercise
%Here is another attempt at a construct equivalent to the above.
%Will it work? Why or why not?
%local T in
% local A B C D in
% {Label T} = tree
% T.key = A
% T.left=B
% T.right=C
% T.value=D
% A = 1
% B = 2
% C = 3
% D = 4
% end
% {Browse T}
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Extracting values from a compound data structure %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
local T A in
T = tree(key:"seif" age:48 profession:"professor")
tree(key:A ...) = T
{Browse A}
end
%Note : The browser will display a list of ASCII values. To view that
%as a string, click on Options, Representation and then String
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%Avoiding Memory Leaks %
%%%%%%%%%%%%%%%%%%%%%%%%
local fun {Sum X L1 L}
case L1 of Y|L2
then {Sum X+Y L2 L}
else X
end
end
in local L = [1 2 3] in
{Browse {Sum 0 L L}}
end
end
local fun {Sum X L1}
case L1 of Y|L2 then {Sum X+Y L2}
else X
end
end
in local L = [1 2 3] in
{Browse {Sum 0 L}}
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%