| CS 330 | Home | Schedule | Resources |
Estimated time: 4 hours
The objective of this homework is to familiarize yourself with the declarative model and its semantics using the kernel language.
proc {P X}
if X>0 then {P X-1} end
end
Is the second occurrence of the identifier P free or bound? Justify your answer by first translating to kernel syntax. (Tangential hint: To translate infix operators into the kernel language, you can use the online Mozart Documentation's alphabetical index under 'Symbols', before 'A', to find out which module defines each operator.)
declare MulByN A=10 B N in
N=3 % *
proc {MulByN X ?Y} % *
Y=N*X % *
end % *
{MulByN A B}
{Browse B}
What value, if any, is printed in the Browser? Now modify the code so that the lines marked with an asterisk form the body of a local statement that declares a new identifier N. Consider two scenarios: one where the identifier N is removed from the declare line, and another where it remains and is bound to 4. How does the overall execution change? Explain why in terms of what is happening to the environment when MulByN is called.
[Clarification: in the second part of this question, "Now modify...," you're comparing two different versions of the above code:
-Max]
N and no N in the declare statement
N and one N in the declare.
[Clarification: for purposes of this question, it's okay to use values as well as identifiers where the kernel syntax on page 50 has only variables like < x1>. For instance, instead of "local X in X=21 person(age:X)", you should write "person(age:21)".]
if and case statements. (10 points) This exercise explores the relationship between the if statement and the case statement.
Label, Arity, and '.' (feature selection).
This shows that the if statement is essentially a more primitive version of
the case statement.
proc {Test X}
case X
of a|Z then {Browse 'case'(l)}
[] f(a) then {Browse 'case'(2)}
[] Y|Z andthen Y==Z then {Browse 'case'(3)}
[] Y|Z then {Browse 'case'(4)}
[] f(y) then {Browse 'case'(5)}
else {Browse 'case'(6)} end
end
Report what happens when you execute each of the following:For your own benefit, make sure you understand exactly what happens in each case.
proc {Test X}
case X of f(a Y c) then {Browse 'case'(l)}
else {Browse 'case'(2)} end
end
Without executing any code, predict what will happen when you feed each of the following: - declare X Y {Test f(X b Y)}
- declare X Y {Test f(a Y d)}
- declare X Y {Test f(X Y d)}
Use the kernel translation and the semantics if necessary to make the prediction. After making the predictions, check your understanding by running the examples in Mozart. Now run the following example:
declare X Y
if f(X Y d)==f(a Y c) then {Browse 'case'(l)}
else {Browse 'case'(2)} end
Does this give the same result as the previous problem, or a different one?
Explain the result.
declare Max3 Max5
proc {SpecialMax Value ?SMax}
fun {SMax X}
if X>Value then X else Value end
end
end
{SpecialMax 3 Max3}
{SpecialMax 5 Max5}
See what happens when you execute:
{Browse [{Max3 4} {Max5 4}]}
What is Max3? How is it different from Max5?
fun {AndThen BP1 BP2}
if {BP1} then {BP2} else false end
end
Does the call {AndThen fun{$} expression1 end fun {$} expression2 end}
give the same result as expression1 andthen expression2? Does it avoid the
evaluation of (expression)2 in the same situations?
orelse as AndThen is to andthen.
Explain its behavior.