Recursion eliminates the need for loops to be typed out in code, simplifying the code for anyone reading it. Rather than requiring a do while, while, or for loop, a function can simply include a conditional or non-conditional call to itself, effectively stacking a new occurance of the function which starts at its beginning.
The book provides the example of this loop approach
procedure gcd(u,v: in integer; x: out integer) is
y, t, z: integer;
begin
z := u;
y := v;
loop
exit when y = 0;
t := y;
y := z mod y;
z := t;
end loop;
x := z;
end gcd;
And in contrast, the recursive version
function gcd(u, v: in integer) return integer is
begin
if v = 0
return u;
else
return gcd(v, u mod v)
end if;
end gcd;
The improved code efficiency and readability is immediately obvious
Wednesday, February 29, 2012
1.10
An index based loop is limited in its use with an array in that extra work must be done to ensure it does not attempt to access data outside bounds of the array. This can be accomplished by using a variable which is updated alongside the array which tracks its length(for a dynamic array) or a constant which specifies its length(static array). This makes an index based loop highly sensitive to changes in the array. In contrast, an iterator should include checks to ensure it does not leave the bounds of the array(such as .hasNext). An iterator is overall more predictable than an index based loop.
Saturday, February 18, 2012
2.22
1: Design, don't hack
a) This first point means that when designing a language, one should include consistent and predictable methods for accomplishing expected tasks rather than including workarounds and messy tricks to get things done.
2: Study other designs
a) This one is somewhat self-explanatory
3: Design top-down
Look at the big picture first, then flesh out the specification
4: Know the application area
Understand your audience, and understand what the language needs to be able to do
5. Iterate with respect to design
C++ fulfilled all goals
a) This first point means that when designing a language, one should include consistent and predictable methods for accomplishing expected tasks rather than including workarounds and messy tricks to get things done.
2: Study other designs
a) This one is somewhat self-explanatory
3: Design top-down
Look at the big picture first, then flesh out the specification
4: Know the application area
Understand your audience, and understand what the language needs to be able to do
5. Iterate with respect to design
C++ fulfilled all goals
Sunday, February 5, 2012
1.14
The map function applies a specified function to each individual element of a specified list. It allows the programmer to apply a function to many objects with a single line of code, and in a non-looping form, meaning that in a system which supports parallel processing, multiple elements can have the function applied to them simultaneously. This provides additional abstraction capability in that writing a single line of code to apply a function to multiple objects(possibly simultaneously) is far more efficient to write(and possibly to run) than a loop which applies the function to one object at a time and requires significantly more code to write.
Subscribe to:
Posts (Atom)