filter(is_even, squares)
map( square, list(1,2,3) );
accumulate(plus, 0, squares)
function accumulate(op,initial,sequence) {
if (is_empty_list(sequence)) {
return initial;
} else {
return op(head(sequence),
accumulate(op,initial,
tail(sequence)));
}
}
var squares = build_list(10, square);
var even_squares = filter(is_even, squares);
var half_even_squares
= map(function(x) { return x / 2; },
even_squares);
var result = accumulate(plus, 0, half_even_squares);
function flatmap(proc, xss) {
return accumulate(append,
[],
map(proc, xss);
}
function permutations(s) {
if (is_empty_list(s)) {
return list([]);
} else {
return flatmap(function(x) {
return map(function(p) {
return pair(x,p);
},
permutations(remove(x,s)));
},
s);
} }
var hw = "Hello World!";
var hw = "Hello \"World\"!";
var hw = "Hello \\ World!";
var hw = 'Hello World!';
var hw = 'Hello "World"!';
var hw = "Hello" + "World!";
var hw = "The result is: " + 42;
function member(v, xs){
if (is_empty_list(xs)) {
return [];
} else {
if (v === head(xs)) {
return xs;
} else {
return member(v, tail(xs));
}
}
}
function deriv(exp,variable) {
if (is_number(exp)) {
return 0;
} else if (is_variable(exp)) {
return (is_same_variable(exp,variable)) ? 1 : 0;
} else if (is_sum(exp)) {
return make_sum(deriv(addend(exp),variable),
deriv(augend(exp),variable));
} else if (is_product(exp)) {
return make_sum(make_product(multiplier(exp),
deriv(multiplicand(exp),variable)),
make_product(deriv(multiplier(exp),variable),
multiplicand(exp)));
} else {
return "unknown expression type: " + exp);
}
// d x+y / d x
deriv(list("+","x","y"),"x")
// d (0 x + 1 y) / d x
deriv(list("+", list("*", "x",0),
list("*", 1 ,"y")),
"x")
function make_sum(a1,a2) {
if (is_number_equal(a1, 0)) {
return a2;
} else if (is_number_equal(a2, 0)) {
return a1;
} else if (is_number(a1) &&
is_number(a2)) {
return a1 + a2;
} else {
return list("+",a1,a2);
} }
function make_product(m1,m2) {
if (is_number_equal(m1,0) || is_number_equal(m2,0))
return 0;
else if (is_number_equal(m1,1))
return m2;
else if (is_number_equal(m2,1))
return m1;
else if (is_number(m1) && is_number(m2))
return m1 * m2;
else
return list("*",m1,m2);
}
/
#