JavaScript and SICP

xiaoxiao2021-03-06  45

These two days of the company's projects reported a paragraph, and the school's homework is not too difficult. So I am a little leisure, so I will focus on SICP, and I have learned JavaScript (see Google Suggest, Google Maps, and Canyonbridge's CBConnect, which is very interested in JavaScript). Douglas CROCKFORD is very good, JavaScript is indeed similar to Scheme. The author of JavaScript must make a familiar function programming, otherwise the function of JavaScript is a lambda operator too coincident. For example, the NEWTON-RAPHSON algorithm for calculating the square root in SiCp1.1:

(Define (Good-Enough? Gues) (ABS ((Square Guess x)) 0.001) (Define (Define)) SQRT-ITER GUESS (IF (Good-Enough? Guess) Guess (SQRT-ITER 1.0)) (SQRT-ITER 1.0)))

Change to the JavaScript version is close to machinery:

// NEWTON's method to find / sqrt {x} function sqrt (x) {function sqrt_iter (guess) {if (gues) {Return Guess;} Return SQRT_ITER (IMPROVE (GUESS)

Function Improve (Guess) {Return Average (Gues, X / Gues);

Function average (x, y) {RETURN (X Y) / 2;}

Function good_enough (guess) {return math.abs (guess * guess - x) <0.001;}

Return SQRT_ITER (1.0);

Oh, it seems that it is interesting thing to rewrite the program in SICP in JavaScript.

Oh, it seems that it is interesting thing to rewrite the program in SICP in JavaScript.

转载请注明原文地址:https://www.9cbs.com/read-51783.html

New Post(0)