10 ms·
Composing Programs – Python 3 in the tradition of SICP
- chris11 11y agoI was kind of hoping to see python used in the functional programming section. I was playing around with a small project in python and decided I wanted to re-implement a really simple context free grammar I had done in a functional programming class. It was slightly annoying, Lisp seemed like a better choice than python. But it looks like they use scheme.
- jarcane 11y agoPython is deliberately bad at functional programming by design. Guido has decreed it.
- tomsthumb 11y agoIt seems to be pretty good at map, filter, any, all, reversed, reduce, zip, list comprehensions, dict comprehensions, lambdas, and closures. Granted, it would be unusual to see all, or even most, of those in one place, but they see use enough that calling python bad a functional programming is a little odd. I'd say it's pretty good at functional work in the small while somewhat ill suited in the large.
- Sirenos 11y agoJust because it has those functions doesn't make it good at them. Have you ever seen a Pythonic program converted into functional style? It looks horrendous.
- aidos 11y agoI would imagine it depend on what extreme you take it to. You don't have pattern matching or tail recursion, which make certain styles impossible. There's a much better discussion over here http://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming http://stackoverflow.com/questions/1017621/why-isnt-python-v...
- icebraining 11y agoIf you define "functional style" as using immutable data structures, higher-order functions, avoiding side-effects and using referentially transparent variables, I think Python programs look fine, and I try to write all the code I can this way (although practicality beats purity). If you define "functional style" as writing Lisp or Haskell programs using Python syntax, I agree they look horrendous.
- cutler 11y agoOr you can use Hy (http://hylang.org http://hylang.org) and write Python in Lisp syntax.
- tome 11y agoWhere did you get immutable data structures from in Python, and what do you do when every third party library you interact with uses mutable ones?
- icebraining 11y agoBesides tuples, I mostly treat the standard data structures as if they were immutable, without relying on them actually being :) As for third-party libraries, very few of them actually mutate structures passed by my code, in my experience; if they do, I simply make a copy before passing it.
- devishard 11y ago> Where did you get immutable data structures from in Python Here's my favored source (in the Standard Library) https://docs.python.org/2/library/collections.html#collections.namedtuple https://docs.python.org/2/library/collections.html#collectio... > and what do you do when every third party library you interact with uses mutable ones? That's situational; which library are you having a problem with? I haven't really had that problem (okay, sometimes the tooling around web frameworks do stupid things, but that's not common). Note that just because something is mutable doesn't mean you have to mutate it. Yes, Python doesn't as widely enforce non-mutation as Haskell, OCaml, etc., but that doesn't mean that suddenly all the Python programmers lost their minds and started mutating all the things. We're not savages.
- BWStearns 11y agoPython list/dict comprehensions are awesome. The only other language I've seen that has comparably awesome comprehensions is Haskell which (opinion ahead) offers easier access to more power at the cost of some readability upfront. Sadly I've seen functional operations like map/filter/reduce etc discouraged in (commercial) projects I've worked on, though I guess there's a bit of an argument there for debugability (make it a word?) since hunting down side effects may be more onerous?
- sago 11y agoI think the logic was that comprehensions should replace map and filter for most cases. [fn(a) for a in seq] for example or [a for a in seq if pred(a)] and so map and filter are 'old'-style and deprecated. But I have to agree, I think that's not a clear benefit. For example map(fn, seq) and filter(pred, seq) are clearer. Once you get more complex, however, the comprehension syntax does shine. Comprehensions don't help at all with reduce, of course, which has been tidied out of __builtins__ now.
- hvm 11y agoI think it's a matter of taste or what you're used to. I find the first two examples clearer than the map/filter ones. Are map and filter more concise? Yeah but only in this case. If you would need to do some processing, they would get messy fast because of a lambda.
- emidln 11y agoIn my experience, the more complicated processing ends up being composed up of smaller units (that are testable), and then map/filter remain the most clear way of representing the iteration. Using lambdas for non-trivial process is just as bad as throwing that same complicated processing into a list comp.
- sago 11y agoI work with a lot of people who have never drunk the functional koolaid, professionally. They have to do an extra mental step with map or filter - "oh map, that's where the first argument is applied to each item in the second - I think - perhaps the second argument is applied to the first, right, Stack Exchange". More explicit is clearer, then. 'Clear' like beauty, is subjective. But productivity isn't. And whatever my personal feelings, the productivity of the team wins. On the second point. I agree. Python's lambdas are a code-smell, for me. Which I don't say lightly, I'm a Scheme/Lisp guy deep down. But they fundamentally work against the aesthetics of the Python language, imho. A local function declaration is much less fragile. But I would say that as code complexity increases, in my experience, comprehensions don't last long either. They've a narrow range of applicability before you get a big block of spaghetti code. Then it is much better to define a local function and, essentially, 'map' it (even if that 'map' is done as comprehension). Of course, YMMV.
- Walkman 11y agoIt's really not good. Python is an imperative language and if you dig a little, you can find why [0]. The reduce function has been moved to functools module [1]. If you try to write in purely functional style in Python, you will have a bad time. [0]: http://python-history.blogspot.hu/2009/04/origins-of-pythons-functional-features.html http://python-history.blogspot.hu/2009/04/origins-of-pythons... [1]: https://docs.python.org/3.2/library/functools.html#functools.reduce https://docs.python.org/3.2/library/functools.html#functools...
- devishard 11y agoThis is silly. You're pointing out some historical reasons why some functional features have been removed, nerfed, or moved out of __builtins__, but none of this is a discussion of problems you've come across with actually writing Python in a functional style. I've written tens of thousands of lines of code in a functional style in Python, and while it's different from Scheme and takes a little getting used to, Python is a very capable programming language for functional programming. It does lack tail call optimization, but I think you'd find that even in Scheme, most times where you would write a recursive function, you should be writing something with one of the higher-order functions. 99.9% of recursive functions contain a buggy hand-rolled implementation of map, reduce, or filter. Scheme is a more academic language, so it makes sense for them to include this feature, which gives language users the same power as the language creators. But Python is intended for professional development and as such, it makes sense to force users into a better style.
- jarcane 11y agoYes, Erlang is such a toy language[1], it's never used for "professional development" at all[2]. [1] http://learnyousomeerlang.com/recursion http://learnyousomeerlang.com/recursion [2] http://www.fastcompany.com/3026758/inside-erlang-the-rare-programming-language-behind-whatsapps-success http://www.fastcompany.com/3026758/inside-erlang-the-rare-pr...
- devishard 11y ago> Yes, Erlang is such a toy language[1], it's never used for "professional development" at all[2]. From your sarcastic tone it seems like you take issue with something you think I've said, but since your post is unrelated to anything I actually said, I can only guess that you've inferred some meaning that isn't there.
- xor-xor 11y agoAre you sure about lambdas..? They are kind of crippled in comparison to almost any other language which has them.
- itsmemattchung 11y agoI'm currently self studying SICP (https://mitpress.mit.edu/sicp/ https://mitpress.mit.edu/sicp/) and glad this is illustrated in Python.
- dschiptsov 11y agoThose who have mastered principles doesn't have to memorize particulars. Nevertheless I still maintain that the classic CS61a by Prof. Brian Harvey is a gold standard. It teaches the most important big ideas, such that code is data and hence the whole OO paradigm is mere a DSL over structured data with named slots, with a protocol to follow (inheritance). It teaches the superiority of declarative over imperative approach - one defines what shall be done, not how it can be done. It also teaches lazy evaluation, so one doesn't parrot nonsense about monads and Haskell. It teaches what genetics are, and that everything could be defined as an ADT with corresponding predicates (which represents mental categories we learn from environment).
- topkekz 11y agoThe "classic" course is still available as cs61as http://www.cs61as.org/ http://www.cs61as.org/
- dschiptsov 11y agoNo. The different instructor ruined it. Mr. Harvey is a gentle intellectual with charm and that characteristic lack of arrogance and tendencies to show off modern narcissistic personages exhibit nowadays. I can't stand guys like Hickey or Tellman or whoever it is. Watch him, at least first 3 or 4 lectures. https://m.youtube.com/playlist?list=PL1AE464371B3398CC https://m.youtube.com/playlist?list=PL1AE464371B3398CC
- zodiac 11y ago61as is not the same as 61a
- e12e 11y agoIs this the correct link for 61a as referred to by grand parent?: http://wla.berkeley.edu/~cs61a/sp11/ http://wla.berkeley.edu/~cs61a/sp11/
- dschiptsov 11y ago
- sumukh1 11y agoThis is the textbook used for the introductory CS course (CS61A) at Berkeley. The course material is available at http://cs61a.org http://cs61a.org (The course uses Python 3, Scheme, and SQL). There are some neat projects (students are working on building a Scheme interpreter now) Previous Hacker News Discussions: https://news.ycombinator.com/item?id=3491142 https://news.ycombinator.com/item?id=3491142 https://news.ycombinator.com/item?id=3141996 https://news.ycombinator.com/item?id=3141996 Full disclosure: I'm a TA for the course right now.
- cutler 11y agoGiven Python's limited lambda implementation and Guido's distaste for FP I find it odd that Python should be chosen to teach SICP let alone replace Scheme as a teaching language.
- pmoriarty 11y agoThere's a lot of pressure to teach languages that are widely used in industry, which is why so many students come out of university knowing mostly Java. There's also a lot of bias against "old" languages, and for newer, trendy languages. Teaching SICP in Python is just a further development of that trend. Software engineering is a very trend-following, path of least resistance, bandwagon-jumping profession. If "everyone" is using language X, that's where most engineers (and managers) want to be. Universities are just satisfying that need. It's a wonder that Scheme lasted in universities as long as it did. It'll be interesting to see how long Python lasts.
- cutler 11y agoThere's always Clojure. Best of both worlds, i.e. Scheme-ish with Java host. We had a SICP Clojure group here in London last year and I think someone has written an adaptation of SICP in Clojure. If it was any other text I'd have nothing to say but SICP without the Scheme/Lisp doesn't make sense. If universities choosing Python or Java as their teaching language they should also switch to texts based on OOP.
- x5n1 11y agoShould have hosted it on a shared provider, it would still be online.
- deleted 11y ago[deleted]
- nickysielicki 11y ago> Over Quota Archive.org Mirror: http://web.archive.org/web/20160402152716/http://www.composingprograms.com/ http://web.archive.org/web/20160402152716/http://www.composi...
- uptownfunk 11y agoGuys I just have to give the obligatory shout out to Prof Hilfinger. The guy is legendary. My life has been made worthwhile to have been initiated into the deep mystic art of CS by him.
- pgbovine 11y agoi dunno, i'm on the side of Prof. DeNero (who runs this course website) in this rap battle: https://www.youtube.com/watch?v=VTcWL2AfT7k https://www.youtube.com/watch?v=VTcWL2AfT7k
- uptownfunk 11y agoNo way man it's all about Hilfinger, probably the only Prof who still highly encourages the use of emacs at Cal.
- jxy 11y agoBerkeley CS bends to common usage, instead of holding to its own pride? Scheme is such a simple but deep language, and is really good at introducing all the concepts. It is aged, like a good single malt whisky. Now they try to serve fruit punch?
- toolslive 11y agoI wonder what Dijkstra (if he were still alive) would have to say about this.
- thramp 11y agoI love Scheme as much as anybody else, but I’m not convinced that it should be the first introduction to computer science that people have. I think that the goals for an intro class is to get people to enjoy programming, not expose them to the raw beauty of Scheme/FP. I sure as shit didn’t appreciate what Scheme offered the first time I learned it.
- raymondh 11y agoOverall, SICP in Python 3 is first rate. I highly recommend it to anyone wanted to improve their sophistication with Python and programming in general. The exercises and demonstrations in each section quickly build from an elementary introduction up to powerful examples accompanied by clear explanations. I especially like the explanatory diagrams. Thank you to the authors for your craftsmanship and for making it available on-line.
- gagagababa 11y agoI was thinking of taking "Principles of Reactive Programming" by Martin Ordesky after taking his other class "Functional Programming Principles in Scala" on Coursera. After looking at CS 61A, I am thinking of taking it instead. How long did it take you to complete the course? Also, as far as I understood you can use the grader with "--local" flag to check the correctness of your program, is that the case?
- sumukh1 11y ago> as far as I understood you can use the grader with "--local" flag to check the correctness of your program, is that the case? Yes. The autograder is provided with each assignment.
- alexkon 11y agoHas anyone read this book? What’s your experience? Would it make an approachable introduction to programming for a complete newbie?