...
Is this version relatively efficient?
In general, you can take any program using local, and turn it into an equivalent program without local.
Using local doesn't let us write programs which were impossible before, but it does let us write them better.
...
Generating a list of
...
ascending numbers
...
Retrieve your code for the up
function and its helper upFrom
from last lab.
Rewrite this program so that the helper function upFrom
is hidden and takes only one argument since the upper bound argument is available as the parameter to the enclosing definition of up
We'll develop functions returning the list of positive numbers 1...n (left-to-right), given n as input.
Develop a function that, given n and i, normally
returns the list of length i of numbers up to n: (list n-(i-1) ...n-1 n)
More concretely, e.g.,
(nums-upto-help 5 0) ? empty(nums-upto-help 5 2) ? (list 4 5)
(nums-upto-help 5 5) ? (list 1 2 3 4 5)
(nums-upto-help 6 5) ? (list 2 3 4 5 6)
(nums-upto-help 7 5) ? (list 3 4 5 6 7)
For simplicity, you may assume that i=n.
This should just follow the template for natural numbers on i.
Yournums-upto-help repeatedly passed one argument unchanged to its recursive call. I.e., it's an invariant argument.
Rewrite your function, adding a helper, hidden by local, which avoids having an invariant argument.
Don't forget to include a contract and purpose for this new local function, too.
Develop the function we really want:
(nums-upto 5) => (list 1 2 3 4 5)
Use yournums-upto-helpto do most of the work.
Edit your code to use local to hide the helper nums-upto-helpinside your main function nums-uptothe revised upFrom
function.
Advice on when to use local
...