Download 1 Introduction

Transcript
% Syntax Convenience: functional notation
local
fun {AndThen BP1 BP2}
case {BP1} then
case {BP2} then true else false end
else false end
end
fun {BinaryTree T}
case T
of nil then true
[] tree(K V T1 T2) then
{AndThen fun {$}{BinaryTree T1} end
fun {$}{BinaryTree T2} end}
else false end
end
end
Figure 10: Checking a Binary Tree lazily
Move the result variable into the inner case-expression to make it a case-statement,
and we are done:
proc {AndThen BP1 BP2 B}
case {BP1} then
case {BP2} then B = true else B = false end
else B = false end
end
If you are a functional programmer, you can cheer up! You have your functions,
including higher-order ones, and similar to lazy functional languages Oz allows certain forms of tail (recursion) optimizations that are not found in certain (strict7 )
functional languages including Standard ML, SCHEME and the concurrent language
Erlang. However functions in Oz are not lazy. This property is easily programmed,
using the concurrency constructs and the variables. Here is an example of the well
known higher order function Map. It is tail recursive in Oz but not in Standard ML
nor in SCHEME.
declare
fun {Map Xs F}
case Xs
of nil then nil
[] X|Xr then {F X}|{Map Xr F}
end
end
{Browse {Map [1 2 3 4] fun {$ X} X*X end}}
andthen and orelse. After all we have been doing a lot of work for nothing!
Oz already provides the Boolean lazy (non-strict) version of the Boolean function
7
A strict function evaluates all its argument rst before executing the function.
21