4 ms·
(define (deriv f) (define (f-prime x) (/ (- (f (+ x dx)) (f x)) dx)) f-prime) jeez, I wish it was easy to do that in Python.
by danbmil99 18y ago
(define (deriv f)
(define (f-prime x)
(/ (- (f (+ x dx)) (f x))
dx))
f-prime)
jeez, I wish it was easy to do that in Python. Python's main limitation, IMSHO, is lack of homoiconicity. Yes, I said it!
- danbmil99 18y agoactually, you can do it pretty easily with lambda... but it's just not the same somehow. You can't (in the general case) use Python to generate Python, except at the text level.
- sharkbrainguy 18y agoI hate to be the guy who's always saying the same thing but, that is easy in python. dx = 0.00001 def deriv(f): return lambda x: \ (f(x + dx) - f(x))/dx In [11]: def cube(x): ....: return x*x*x ....: In [12]: deriv(cube)(5) Out[12]: 75.000149996640175 I'd also so that most (high school) educated people find infix easier to read than prefix, so isn't the homoiconicity a detriment to readability in this example (and any involving mid sized arithmetic expressions). I love scheme (alot more than I do python), but the only example that seems "impossible" to do is to implement a large subset of <language> in <language>. You could obviously implement, a scheme-ish lisp in python, but there are alot of advantages to the host and guest languages being similar (or so that berkley professor would have me believe) that you would miss out on. (You would also need a scheme expression reader in python... which I'm just itching to write now ... ) the example is from sicp: http://tinyurl.com/cubk49 http://tinyurl.com/cubk49