break a big program into several small pieces and construct them sperate
by using call function we can make a interative structure without any loop syntax
it is crucial that each function accomplishes an** identifiable task** that can** be used as a module** in defining other functions. (how to finish the biggest task? break to into pieces, how to finish the pieces?…)
Linear Recursion(shape): a chain of defered operations, the interpreter keeps track of the operations to perform later on. the chain has n length. time O(n) storeO(n)
linear iterative: no need to grow and shrink. state can be summarized by a fixed number of state variables, together with a fixed rule that describes how the state variables should be updated as the process moves from state to state and an (optional) end test that specifies conditions under which the process should terminate.time O(n) store O(1)
the difference: iterative process no need the interpreter to store some “hidden” state, all description are provided with state. And the recrusive process store state with the help of interpreter, the longer the chain is, the more state the interpreter should maintain.
recursive function: means a function syxnaxly refer itself, it has nothing to do with recursive process. a recursive function can be iterative process as well.
recursive process: it s a way of function evolves, leave the state to interpreter, and call the next function.
tail-recrusive: common language consume space even in iterative process’ recursive function. But js has tail-recrusive optimization, it consume constant space. With a tail-recursive implementation, iteration can be expressed using the ordinary function call mechanism, so that special iteration constructs are useful only as syntactic sugar.
function call itself twice, it will form a binary-tree. we can anaysls O using tree graph.(it’s all about how to evolue function upon some ohter functions.)
tree recursion is useful in tree structure data or graph, and more straightforward than interative process
change to interative process: thinking buttom to top, f(n) = f(n-1)+f(n-2), so f(n) + f(n-1) = f(n+1), we need two state to store n, n-1, and a limit condition count
1 | function fib(n){ |
Counting change
How many different ways can we make change of $1.00 (100 cents), given half-dollars, quarters, dimes, nickels, and pennies (50 cents, 25 cents, 10 cents, 5 cents, and 1 cent, respectively)?
1 | // problem: given 100 or n, count the ways of splitting it in |
Exercise 1.11
1 | // recursive |
Exercise 1.12
1 | // consider p[i][j] |
1 | // fast expt: given b and n, calculate b^n |
辗转相除法:个人理解, 把a和b看成m份x和n份x,其中x为GCD(a, b),因为是最大公约数所以显然可以把a和b分成m/n份。a % b = r, = m * x - r * n * x = g * x, 他们的最大公约数仍然是x
1 | // GCD(a, b) = GCD(a, r) r = a % b |
see the different process generated by normal order evaluation and applicate order evaluation
divisors test
1 | function is_prime(n) { |
The fermat test
1 | // if n is prime, then a^n mod n = a mod n, if 0 < a < n. O(time*logn) |
1 | // for testing the time cost of the function, we make a high order function |
consider those three functions below:
1 | function sum_integers(a, b) { |
We can extract most of the part as a new function, and leave the difference as specify functions ‘term’ and ‘next’:
This is the summation in math:
Exercise 1.29
1 | function summate(a, b, f, next) { |
Exercise 1.31
1 | function product(a, b, f, next) { |
Exercise 1.32
1 | // use same thought to extract common partern of summate and product function |
given f(x), find the root of f(x) = 0. also given boundary a,b where root is between [a,b].
solution:
1 | function find_root(f, r, a, b) { |
1 | // example 1: make a damp to a function for instance x -> x * x |
This is a strengthen of expression, without fn as param we can not express abstraction of operation that operate other function. without fn as return value we can not express the abstraction of warping / upgrade a function use extra logic.
1 | // deriv is a new function base on g(x), so we can use fn as return value to |
1 | // given param, generate a function |
1 | function double(f) { |
1 | // abstraction of compose |
1 | // given a function and n times, modify the function to repeat itself n times |
1 | // smoothing function, times |
iterative improvement: in order to compute result, we first start at a initial guess, then judge whether it is good enough, if no, repeat the process.
1 | function iterative_improve(improve, is_good_enough) { |
It is all about increasing your ability of expression.
sicpjs — Apr 25, 2022
Made with ❤ and at Earth.