3 ms·
Just for looksies. function deriv(exp,v){ //v = variable; if(is_number(exp) ){ return 0 } if(is_variable(exp){ return is_same_variable(ex
by 6510 3y ago
Just for looksies.
function deriv(exp,v){
//v = variable;
if(is_number(exp) ){ return 0 }
if(is_variable(exp){ return is_same_variable(exp,v) * 1 }
if(is_sum(exp)){
a = deriv(addend(exp),v);
return make_sum(a,a)
}
if(is_product(exp)){
m = multiplier(exp);
c = multiplicand(exp);
a = make_product(m,deriv(c,v));
b = make_product(deriv(m,v),c));
return make_sum(a,b)
}
return error(exp, "unknown expression type -- deriv");
}
- MrJohz 3y agoI'd possibly make it even more syntactically simple by removing the braces for one-line if statements. I know this can be a bit controversial, but for cases like this where the block consists only of a return statement, and where that statement fits on a single line (and where you have a formatter and a linter that will prevent you from writing indentation that doesn't match the semantics), it's very useful for removing visual clutter. That said, this is pretty much how I'd write it, and seems a lot simpler than the heavily nested ternary (and I say that as a fan of nested ternaries!)