Concept:
Local_scope
Headline
Validity of program identifiers in only the declaring scope
Illustration
Local scope exists in many forms for most programming languages.
Consider the following implementation of the factorial function in Language:Java:
// An imperative definition of the factorial function
public static int factorial(int n) {
// n and result use method scope
int result = 1;
// i uses the for loop as scope
for (int i=n; i>1; i--)
result = result * i;
return result;
}
Several kinds of local scope are present. The parameter of the method, n, has local scope with the entire method corresponding to the scope. The local variable result has that same local scope, but it is declared differently. The index variable i is declared in the for loop and its scope is indeed limited to the for loop.
Consider the following Language:Haskell function for splitting a list into two halves (+/-1 in the case of a list of odd length):
-- Split a list into halves
split :: [Int] -> ([Int],[Int])
split xs = (take len xs, drop len xs)
where
len = length xs `div` 2
Local scope is present again in several ways. First, the argument xs of the split function is locally bound in the given equation; specifically it can be used on the right-hand side of the equation. Further, the split function carries a "where" block which is used to define a local binding len. Again, len can only be used within the scope of split. It is also worth noting that len makes good use of the argument xs which has been bound by the hosting function equation for split.
The use of local definitions like len greatly improves clarity of programming. If we were to avoid the local binding, then the split function would need to duplicate some code as follows:
-- Splitting without using local scope
split' :: [Int] -> ([Int],[Int])
split' xs =
( take (length xs `div` 2) xs,
drop (length xs `div` 2) xs )
There are no revisions for this page.
User contributions
User edits
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.