%%%%%%%%%%%%%%%%%%%%%%%%%%% %Syntactic sugar examples % %%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% local X A B in A = "George" B = 25 X = person(name:A age:B) end local X = person(name:"George" age:25) in skip 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 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 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 end %Exercise 1(Also in last slide of the lecture) %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 %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 end 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 end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Exercise 2.1 (Also in the last slide of the lecture) %Here is a construct that uses some syntactic sugar: %local T in % local tree(left:A right:B) = T in %end %Here is an attempt to write the same construct using only the kernel language: %local T in % local A B in % {Label T} = tree % T.right = B % T.left = A % % end %end %Are the above constructs always equivalent? Can you find a case when they are not? If not, how will you justify your belief that they are equivalent? %Exercise 2.2 (Also in the last slide of the lecture) %Now suppose that A and B are predefined variables and Func1 is a predefined one argument function %Here is a construct that uses some syntactic sugar: %local T in % tree(left:{Func1 A} right:{Func1 B}) = T %end %Here is an attempt to write the same construct using only the kernel language: %local T in % {Label T} = tree % T.right = {Func1 B} % T.left = {Func1 A} %end %Are the above constructs always equivalent? %How will your answer change if you are told that neither A nor B is unbound? %How will your answer change if you are told that neither A nor B is unbound, and Func1 never throws an exception? %How will your answer change if you are told that neither A nor B is unbound, and Func1 is a lamda combinator? %How will your answer change if you are told that neither A nor B is unbound, and Func1 never throws an exception, and Func1 is a lambda combinator? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %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 skip end local fun {Sum X L1} case L1 of Y|L2 then {Sum X+Y L2} else X end end in skip end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%