Dynamic Scope
As a static scope language, Jaskell supports dynamic scope with a few functions.
- define is the fundamental function that enables dynamic scope.
With define, one can place a java.util.Map or a tuple as the scope and all the members in the map or tuple are automatically visible to the following expression. For example:
> define {a=1;b=2} (a+b)
>
=> 3
Though straight-forward, the above example isn't normally very useful. We could always use:
Yet, define and its variants are more useful when the scope is dynamically determined. For example, the tuple or map can be provided by user at runtime.
- with is a function derived from define. It is used to make visible all public methods and fields of an object to the following expression.
For example:
with "abc" (charAt[1]-charAt[0])
is equivalent to the following java code:
"abc".charAt(1)-"abc".charAt(0)
If you are familiar with the Basic language, this is essentially the "with" statement that allows us to invoke methods without explicitly writing down the object again and again.
- do is another function derived from define. It is to mimic the monadic do notation where higher-order monadic combinators can be combined in an intuitive syntax. For example:
do {v1 = cmd1} $
do {v2 = cmd2} $
return (v1+v2)