Last active
December 22, 2015 13:59
-
-
Save jin/35d407f2c753c4a35a40 to your computer and use it in GitHub Desktop.
DG3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// integration using Simpson's Law | |
function calc_integral (func, a, b, n) { | |
var h = (b - a) / n; | |
function helper (func, acc, k) { | |
if (k === n) { | |
if (k % 2 === 0) { | |
return (acc + 2 * func(a + k * h)); | |
} else { | |
return (acc + 4 * func(a + k * h)); | |
}; | |
} else { | |
if (k % 2 === 0) { | |
return helper(func, acc + 2 * func(a + k * h), k + 1); | |
} else { | |
return helper(func, acc + 4 * func(a + k * h), k + 1); | |
}; | |
}; | |
} | |
return h/3 * (func(a + 0 * h) + helper(func, 0, 1)); | |
} | |
display(calc_integral(function(x) { return Math.pow(x, 3); }, 0, 1, 100)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//translate sum() from a recursive process to an iterative process | |
var term = function(x){return x;}; | |
var next = function(x){return x + 1;}; | |
function sum(term, a, next, b){ | |
if (a > b) { | |
return 0; | |
} else { | |
return term(a) + sum(term, next(a), next, b); | |
}; | |
} | |
function sum_iter(term, a, next, b) { | |
function iter (a, result) { | |
if (a > b) { | |
return result; | |
} else { | |
return iter(next(term(a)), result + term(a)); | |
}; | |
} | |
return iter(a, 0); | |
} | |
display(sum(term, 1, next, 10)); | |
display(sum_iter(term, 1, next, 10)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//define a function g that represents the big-PI function | |
function fold(op, f, n) { | |
if (n === 0) { | |
return f(0); | |
} else { | |
return op(f(n), fold(op, f, n - 1)); | |
}; | |
} | |
var g = function(k){return (k - Math.pow((k + 1), 2));}; | |
var op = function(x, y){return x * y;}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// abstract the sum() function from Q2 to generalize it. | |
var term = function(x){return x;}; | |
var next = function(x){return x + 1;}; | |
var combiner = function(x, y){return x + y;}; | |
function accumulate (combiner, null_value, term, a, next, b) { | |
if (a > b) { | |
return null_value; | |
} else { | |
return combiner(term(a), accumulate(combiner, null_value, | |
term, next(a), next, b)); | |
}; | |
} | |
// iterative version of accumulate() | |
function accumulate_iter(combiner, null_value, term, a, next, b) { | |
function iter (result, a) { | |
if (a > b) { | |
return result; | |
} else { | |
return iter(combiner(result, term(a)), next(a)); | |
} | |
} | |
return iter(null_value, a); | |
} | |
display(accumulate(combiner, 0, term, 1, next, 10)); | |
display(accumulate_iter(combiner, 0, term, 1, next, 10)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// an accumulate function that takes in a filter. | |
var term = function(x){return x;}; | |
var square_term = function(x){return x * x;}; | |
var next = function(x){return x + 1;}; | |
var combiner = function(x, y){return x + y;}; | |
var is_prime //given function | |
// sum of square of all primes in range a -> b | |
function filtered_accumulate (filter, combiner, null_value, term, a, next, b) { | |
if (a > b) { | |
return null_value; | |
} else if (filter(a)) { | |
return combiner(term(a), filtered_accumulate(filter, combiner, null_value, | |
term, next(a), next, b)); | |
} else { | |
return filtered_accumulate(filter, combiner, null_value, term, next(a), next, b); | |
}; | |
}; | |
display(filtered_accumulate(is_prime, combiner, 0, square_term, a, next, b)); | |
// sum of all positive integers below n, and integer must be prime relative to n | |
var term = function(x){return x;}; | |
var next = function(x){return x + 1;}; | |
var combiner = function(x, y){return x * y;}; | |
var gcd_prime_filter = function(x, y){return ((gcd(x, y) === 1) && (x > 0))}; | |
function filtered_accumulate (filter, combiner, null_value, term, a, next, b) { | |
if (a > b) { | |
return null_value; | |
} else if (filter(a, b)) { | |
return combiner(term(a), filtered_accumulate(filter, combiner, null_value, | |
term, next(a), next, b)); | |
} else { | |
return filtered_accumulate(filter, combiner, null_value, term, next(a), next, b); | |
}; | |
}; | |
display(filtered_accumulate(gcd_prime_filter, combiner, 0, term, a, next, b)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rectangle implementation using abstraction concepts | |
// JSNum, JSNum -> Point | |
function make_point (x, y) { | |
return pair(x, y); | |
} | |
// Point -> JSNum | |
function x_point(point) { | |
return head(point); | |
} | |
// Point -> JSNum | |
function y_point(point) { | |
return tail(point); | |
} | |
// Point, Point -> Segment | |
function make_segment (point, point) { | |
return pair(point, point); | |
} | |
// Segment -> Point | |
function start_segment (segment) { | |
return head(segment); | |
} | |
// Segment -> Point | |
function end_segment (segment) { | |
return tail(segment); | |
} | |
// Segment -> Point | |
function midpoint_segment (segment) { | |
var midpoint_x = (x_point(start_segment(segment)) + x_point(end_segment(segment))) / 2; | |
var midpoint_y = (y_point(start_segment(segment)) + y_point(end_segment(segment))) / 2; | |
return make_point(midpoint_x, midpoint_y); | |
} | |
// JSNum, JSNum, JSNum, JSNum -> Rectangle | |
function rectangle (length, width, x_origin, y_origin) { | |
// top, bottom, left, right | |
var top_segment = make_segment(make_point(x_origin, y_origin), make_point(x_origin + length, y_origin)); | |
var bottom_segment = make_segment(make_point(x_origin, y_origin - width), make_point(x_origin + length, y_origin - width)); | |
var left_segment = make_segment(make_point(x_origin, y_origin), make_point(x_origin , y_origin - width)); | |
var right_segment = make_segment(make_point(x_origin + length, y_origin), make_point(x_origin + length, y_origin - width)); | |
return list(top_segment, bottom_segment, left_segment, right_segment); | |
} | |
// String, Rectangle -> Segment | |
function rectangle_sides (side, rectangle) { | |
if (side === "top"){ | |
return head(rectangle); | |
} else if (side === "bottom") { | |
return head(tail(rectangle)); | |
} else if (side === "left") { | |
return head(tail(tail(rectangle))); | |
} else { | |
return head(tail(tail(tail(rectangle)))); | |
} | |
} | |
// Rectangle -> JSNum | |
function rectangle_width (rectangle) { | |
var width = y_point(start_segment(rectangle_sides("top", rectangle))) - | |
y_point(start_segment(rectangle_sides("bottom", rectangle))); | |
return width; | |
} | |
// Rectangle -> JSNum | |
function rectangle_length (rectangle) { | |
var length = x_point(start_segment(rectangle_sides("top", rectangle))) - | |
x_point(start_segment(rectangle_sides("right", rectangle))); | |
return length; | |
} | |
// JSNum -> JSNum (positive) | |
function abs (x) { | |
return (x < 0) ? -x : x; | |
} | |
// Rectangle -> JSNum | |
function rectangle_perimeter(rectangle) { | |
var width = rectangle_width(rectangle); | |
var length = rectangle_length(rectangle); | |
perimeter = 2 * width + 2 * length; | |
return abs(perimeter); | |
} | |
// Rectangle -> JSNum | |
function rectangle_area(rectangle) { | |
var width = rectangle_width(rectangle); | |
var length = rectangle_length(rectangle); | |
area = width * length; | |
return abs(area); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment