6 ms·
This is a fun demo of functional programming, but Lisp isn't so special anymore. In Python: >>> def deriv(f): ... dx = 0.0001 ... def fp(x): ...
by goldmab 14y ago
This is a fun demo of functional programming, but Lisp isn't so special anymore. In Python:
>>> def deriv(f):
... dx = 0.0001
... def fp(x):
... return (f(x + dx) - f(x)) / dx
... return fp
...
>>> cube = lambda x: x**3
>>> deriv(cube)(2.0)
12.000600010022566
- pmelendez 14y agoWell OP was talking about his experience back in 1983. Scheme is around since 1975 so Python catch that up 16 years later. Beside the fact that you can do that now even in C++, but that's not the point.
- chongli 14y agoExcept Python didn't really catch up. As mentioned below, Python doesn't have tail call elimination so you won't be able to do the same example with the factorial function.
- emiljbs 14y agoCan we please avoid turning this into a "My favourite language can do this too!!"-thread?
- CoffeeDregs 14y agoA great foreign movie might not be as "great" given English subtitles, but I'll still appreciate it's greatness when I have access to it in my favorite/native language. Stretching the analogy..., since Lisp isn't my native language, I very much appreciated the Python "subtitles" provided by the grandparent.
- city41 14y agoThis is my hang up with Lisps over and over again. I read through SICP and did almost all of the exercises. I've played with Clojure and done some small projects in it. I've recently started dabbling in ClojureScript. And I just can't seem to get to the point where Lisp becomes readable and quickly parsable by my brain. That snippet of Python you wrote is immensely more readable than the Lisp in the article. The equivalent snippet in Ruby, JavaScript, C#, Haskell, whatever, would also be immensely more readable than Lisps. Does it just take some serious perseverance? Or am I just Lisp-dumb?
- orthecreedence 14y agoIt seems some people are able to read it, some people aren't. I made it a personal goal a few years back to learn Common Lisp, and while the first month after I started learning was hard, I've never really looked back since then. If I was to go back before that point, I'm sure I would find most languages more readable than lisp, but now that I know it fairly well, the syntax/parens don't bother me at all. I can parse it really easily with a passing glance. Maybe it's just a matter of experience. In the end, a language is a language...if you don't like one, you can use another. I happen to love lisp, but it's a personal choice, and I can understand why people wouldn't like the syntax.
- meric 14y agoDid you also learn emacs in parallel with Common Lisp?
- orthecreedence 14y agoNo, I was learning Vim at the exact same time, so ended up using Slimv (vim equivalent of Slime). I think in my entire life, I've spent about 10 minutes in emacs. Not because I don't like it, but more so because I never took the time to sit down and learn.
- eksith 14y agoNope, you're not. Readability is crucial for those of us not wired the same way as those who seem to be more comfortable in Lisp. Can you get a lot done in Lisp? Sure, if you can scan through and make sense of it. I've tried for a long time, almost to the point of barging in on my work time, but I haven't been able to get comfortable. The innumerable parentheses, for example, make perfect sense to those with the Lisp brain, but since I'm reading it in the context of English, where too many of those are frowned upon, it's nearly impossible for me to follow quickly. Whenever I bring this up among my colleagues, I get shot down by the proficient folks. The argument then turns into how I'm deficient somehow or haven't tried hard enough -- the word "stupid" came up a few times -- and that just completely turned me off the language. Religion is a touchy subject.
- tobinfricke 14y agoYou think that's magic? Wait till you learn about "automatic differentiation," in which you can mechanically and exactly take the derivative of a computer program (function) without a kludge like setting dx=0.001. In the "old days" this took the form of a transformation of the (say, Fortran) source code. In dynamic languages it can be almost trivial to implement. The relevant wikipedia article is here, but it makes the process sound way more complicated than it really is: http://en.wikipedia.org/wiki/Automatic_differentiation http://en.wikipedia.org/wiki/Automatic_differentiation Here's a blog entry about it: http://nibot-lab.livejournal.com/65962.html?thread=108202 http://nibot-lab.livejournal.com/65962.html?thread=108202
- kroger 14y agoMost of SICP can easily be implemented in Python since SICP focus on computational ideas and not on languages features (it doesn't use lisp macros, for instance). The first 3 chapters should look good in most languages with support for high order functions. It starts to get tricky in chapter 4 since Lisp is homoiconic. I started writing about learning SICP using python [1]. Maybe I should continue it. [1] http://pedrokroger.net/2010/08/sicp-in-python-1-1-the-elements-of-programming/ http://pedrokroger.net/2010/08/sicp-in-python-1-1-the-elemen...
- deleted 14y ago[deleted]
- jasongrout 14y agoI had fun implementing "Why Functional Programming Matters" [1] in Python [2], and it turned out to be similar to what you have above. [1] http://www.cse.chalmers.se/~rjmh/Papers/whyfp.html http://www.cse.chalmers.se/~rjmh/Papers/whyfp.html [2] http://sage.cs.drake.edu/home/pub/62/ http://sage.cs.drake.edu/home/pub/62/
- acqq 14y agoOr even Perl: sub deriv { my $f = shift; my $dx = 0.0001; return sub { my $x = shift; return ( &$f( $x + $dx ) - &$f( $x ) ) / $dx; } } sub cube { return $_[0] * $_[0] * $_[0]; } print( deriv( \&cube )->( 2.0 ) ); 12.0006000100226 -------- "Lisp has all the visual appeal of oatmeal with fingernail clippings mixed in." -- Larry Wall (but see also http://www.perl.com/pub/2000/01/10PerlMyths.html#Perl_looks_like_line_noise http://www.perl.com/pub/2000/01/10PerlMyths.html#Perl_looks_...)
- draegtun 14y agoor even TMTOWTDI :) Here in perl5i (https://metacpan.org/module/perl5i https://metacpan.org/module/perl5i): func deriv ($f) { my $dx = 0.0001; func ($x) { ($f->($x + $dx) - $f->($x)) / $dx }; } my $cube = func ($x) { $x ** 3 }; say deriv($cube)->(2); And also in perl6: sub deriv (Code $f) { my $dx = 0.0001; -> $x { ($f($x + $dx) - $f($x)) / $dx }; } my $cube = -> $x { $x ** 3 }; say deriv($cube)(2);
- flyinRyan 14y agoLarry Wall must be one of the most ironic people in the open source world, on purpose or otherwise. He also made a statement like "Lua doesn't have real OO, they just use a hash with a magic function" or something like that....
- draegtun 14y agoThe actual statement Larry Wall gave wasn't that bad... I thought Perl 5 had an oversimplified object system till I saw Lua. In Lua, an object is just a hash, and there's a bit of syntactic sugar to call a hash element if it happens to contain code. Thats all there is. They don't even have classes. Anything resembling inheritance has to be handled by explicit delegation. That's a choice the designers of Lua made to keep the language very small and embeddable. For them, maybe it's the right choice. ref: http://www.perl.com/pub/2007/12/06/soto-11.html http://www.perl.com/pub/2007/12/06/soto-11.html Also IIRC Wall's original Lisp quote starts with him praising Lisp and then ending with this infamous|humorous quip.