9 ms·
Why OCaml? [video]
- jakeogh 11y agoDoes making the py recursive miss the point because py isnt guranteed to be immutable? def sum(list): if list: return list.pop() + sum(list) return 0
- scardine 11y agoPython lacks the tail recursion optimizations common in functional languages like lisp. Seems like the language designers favor iteration instead of recursion, because recursion is limited to a depth of ~1000 on the default implementation (see sys.getrecursionlimit). It is always possible to convert recursive into iterative, and most of the time doing so is trivial. I recommend doing this unless your algorithm is O(log n) or better.
- dorfsmay 11y agoIs that true even with "generator expression" (Python's lazy evaluation)?
- solipsism 11y agoGenerators take the guts of a loop and make them first class. In order to evaluate that generator incrementally, you still need to provide a loop or a recursive function.
- groovy2shoes 11y agoThe OCaml version presented in the video isn't tail-recursive, either.
- jakeogh 11y agoaaah. The recursion limit. So sum() fails on large inputs.
- agumonkey 11y agoThe functional bits of python were a very early addition by a colleague of GvR, he never wanted lambdas, map and such to be in python.
- jakeogh 11y agoFor anyone curious, @ 15 min https://www.youtube.com/watch?v=r75X4Vn_E9k https://www.youtube.com/watch?v=r75X4Vn_E9k discusses a recursive sum() and why getrecursionlimit() is there.
- scardine 11y agoExcelent resource! I could not put it better than Gavin Bong in this talk "Functional Programming in Python for the Uninitiated". Thanks!
- thedufer 11y agoEven if Python did tail-recursion optimization, this implementation isn't tail-recursive so it would fail in the same way. This version is: def sum(li, s=0): if len(li) == 0: return s else: return sum(li[1:], li[0] + s)
- thinkpad20 11y agoI'm not sure if that's something that he wrote or you wrote, but a `sum` function which has a side-effect of emptying its input list seems like a terrible thing. I hope you wouldn't use that in production.
- jakeogh 11y agoWhy's that bad? If there's nothing else the input is needed for, why keep it around?
- lambda 11y agoWhen someone calls that, they wouldn't expect the input to be consumed. >>> list = [1,2,3] >>> sum(list)/len(list) >>> Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero
- jakeogh 11y agoThanks. I keep forgetting py isnt working on a copy of the var. copy.deepcopy() kinda fixes it, but then memory use is doubled, and the recursion limit kills it all anyway. I better go read my py code...
- cobaltblue 11y agoThe only one who knows if the input is needed anymore is the caller, not all callers will agree, so it's bad form to let the called function decide automatically unless it's very clearly marked as a mutating, list-destroying function.
- adrusi 11y agoIt's not bad necessarily to do it that way inline in another function, because there's no way to use it elsewhere without realizing that it has the side effect of emptying the list. However since this is recursive that's not realistically how it would be used. It wouldn't be bad if it were obvious what it did, which it isn't with the name "sum", but would be with the name 'sum_and_empty". For good measure, maybe "sum_in_linear_space_and_empty" is even better. It's also less efficient (although in the context of a recursive sum definition that's not too relevant). Essentially what you're doing here is using the length of the list as the iteration variable — the `i' in `for (i = n-1; i >= 0; i--)'. But `pop()' probably does more than decrement the length field, I imagine it also does some memory management bookkeeping at least. I also take issue with how `list' is mutated and accessed in the same expression. I don't really use python, but I think I recall reading that it in fact guarantees left-to-right evaluation of such expressions. Even still, it's confusing and makes programmers, especially those who write C/C++ where this is undefined behavior, uneasy.
- solipsism 11y agoYou can write functional code in any language, just as you've shown. But your mistake perfectly elucidates the point. When you call a function in a functional language, you know the variable you pass as the parameter won't be mutated. When you call a function in a non-functional language, you only hope it won't. And even when the type system allows you to know the input variable won't be mutated (say, passing by copy in C++), you don't know what other kinds of side-effects the function may be having.
- sampo 11y agoFortran, usually not regarded a functional language, has `pure` keyword to declare that a function (or procedure) has no side effects, and `intent(in)` to declare that a function argument will not be mutated.
- krat0sprakhar 11y agoAlso from Yaron Minsky, in the same vein - http://queue.acm.org/detail.cfm?id=2038036 http://queue.acm.org/detail.cfm?id=2038036
- mark_l_watson 11y agoThanks, great article. I am learning Haskell, but this article on OCaml is generally useful.
- nons3nse 11y agoI am learning Haskell but it is very s-l-o-o-o-w to get to a point where I feel I am actually able to write useful software (probably my fault in fairness!). I have looked over the fence at OCaml and it seems it hits a sweet spot between practicality and rigor. Can anyone advise on whether learning OCaml is worth it when compared to Haskell, considering I am on my way with Haskell, if only slowly? The lack of tooling on Windows (I write cross platform desktop software) and the maturity of the web frameworks (compared to Haskell) has put be off learning OCaml in the past...
- sremani 11y agoIf you are looking for web frameworks, F# (a cousin of OCaml) is a better bet for you compared to OCaml and fits well with your current development environment. F# has Suave.io which is more idiomatic along with asp.net mvc where you can fit F# into. Freya for OWIN - RESTful development looks very nice too. Canopy - for Web UI automation (DSL on selenium) is also an awesome tool.
- gkya 11y agoAFAIK F# is supposed to be an ML that is more like OCaml, but on the .NET runtime. Give it a look, it may be useful for you. Also, IIRC, the compiler for it went OSS recently. But I've not tried the language myself, so this is hearsay.
- dmix 11y agoidk about OCaml but if you find learning Haskell slow I highly recommend learning Erlang. It's similar to Haskell in some way but 10x simpler and highly practical. Probably the most practical language/framework (OTP) I've ever come across outside of maybe Ruby/Rails. By 'practical' I mean in terms of starting out new -> launching a production-worthy business application in an efficient time window. My problem with Haskell is that I spend all of my time trying to build perfect software instead of productively pumping out software. This is why I prefer Erlang, despite the fact Haskell is a superior language in many ways.
- technomancy 11y ago
- jfaucett 11y agoIMHO the biggest factors when evaluating a new programming language boils down to "modules" i.e the methods for creating reusuable units of code, and "package management" i.e. how easy is it require previously built units of code and combine them to create compound units. I wish he had talked more about those aspects. For me, any language that misses these two is not worth the effort unless you have really good reasons (i.e. every clock cycle counts in an embedded program). Anyway, to that end, what is OCaml's packaging ecosystem like?
- yminsky 11y agoThese days, it's pretty good. opam is the tool of choice: http://opam.ocaml.org http://opam.ocaml.org I've found it to be surprisingly reliable and easy to use.
- mbrock 11y agoI'm not very familiar with the actual library distribution stuff in OCaml, but linguistically, the ML style of modules and interfaces is really nice, and sometimes even Haskell programmers envy it. Especially the built-in distinction between an interface and an implementation, which lets you depend on an abstract API rather than on a concrete library.
- a0 11y agoIm my experience OCaml has the best modularity abstractions out there. OCaml modules are unique since they allow you to work with blocks of code (and types) on a higher level. Composing and extending modules, passing modules as parameters to other modules and this way adapting the code to your need. And the nice thing is that all the modules are evaluated at compile-time. Which means you don't have to sacrifice performance for modularity and still get a very flexible framework for Metaprogramming[1]. [1]: https://ocaml.org/learn/tutorials/modules.html#Functors https://ocaml.org/learn/tutorials/modules.html#Functors
- tel 11y agoIt sounds like you're already aware of this from your question and others have already beat me to the punch, but: OCaml modules are best of breed. Better than anything else. Nothing else even comes close. They're weird and different from modules you'll see in other languages. Then after you get used to them you'll find those other modules are at best neutered and most likely severely broken versions of what you've come to know and love from OCaml (or any of the true MLs). OCaml modules arise from a certain treatment of "existential types" which are, in a lot of ways, the very foundation of type abstraction. If you don't care about types practically, then you can care about them spiritually as a means of describing program interfaces. Existential types are maybe the very foundation of abstract program interfaces. So, OCaml is at least 50% good. Opam is kind of nice, too.
- sremani 11y agoMy take aways from the talk: 1. Financial transactions are adversarial and have heavy tails. Testing is not sufficient for Universal Guarantees, type system helps here. 2. Whole class of trivial bugs are not there because of Option (Some, None) and SystemF type system 3. Type system should be part of design process (which happens in OCaml) 4.Precise and Expressive type systems. 5. Right tool for right job, but with OCaml is in sweet-spot to write huge range of programs * little scripts * mini langs for configs * big trading system 6. Teaching/Learning: * a good percentage of traders with a month long bootcamp were able to learn with a good level of competence * MIT and Harvard students were able to achieve competence in 3-4 week internships. 7. The "python" paradox, esoteric language development helps attract high-quality/better talent 8. Tools and libraries OCaml would not be great if you have to reinvent lot of libraries ala. web development, but has good reasonable ecosystem for customized solutions (like trading systems etc.)
- solipsism 11y agoI'm not sure I understand the adversarial part or why that should matter. He mentioned it being a zero-sum game, with some having to lose for others to win. I don't get why that's relevant to the programming language choice. I think what he should have said is that financial transactions are important, and that it's expensive to screw up. This is of course as true or more for, say, NASA's flight software (which is not problem that's not adversarial in the way he means) as it is for financial trading software.
- jleader 11y agoThe difference is that if NASA screws up, there's a certain percentage chance that the bug will be exposed, and cause a problem. That percentage usually stays constant over the lifetime of the system, regardless of how many times the bug has already bitten them. In an adversarial situation, there's a percentage chance that the bug will be exposed, and after that a 100% chance that adversaries will exploit it.
- mmsmatt 11y ago
- brudgers 11y agoRelated: Jeff Meyerson's audio interview with Yaron Minsky. http://softwareengineeringdaily.com/?s=janestreet http://softwareengineeringdaily.com/?s=janestreet It goes into a lot more of the nuts and bolts of JaneStreet and puts the use of OCaml in a more technical context.
- jakeogh 11y agoReading about side effects in py I found: https://github.com/python-effect/effect https://github.com/python-effect/effect
- systems 11y agois multi core ocaml still being considered to merge into the main ocaml distribuition
- agravier 11y agoYes, with an alpha due soon [source: https://www.reddit.com/r/ocaml/comments/3z7zvx/eta_on_403multicore/ https://www.reddit.com/r/ocaml/comments/3z7zvx/eta_on_403mul...]
- e_d_g_a_r 11y agoFor people curious about OCaml and wanting to actually learn the language, aka build something useful, then come to the next OCaml meetup at the Climate Corporation in San Francisco. Its a workshop from idea to opam publishable package. We'll build a command line tool to do bread and butter coding, aka HTTP requests and Json manipulation. You'll have reusable code and a starting point so you don't waste time with building code and actually spend time writing code. http://www.meetup.com/sv-ocaml/events/228367572/ http://www.meetup.com/sv-ocaml/events/228367572/ As incentive I will be giving away Enter the Monad t-shirts courtesy of Jane Street, thank you yminsky!
- agentgt 11y agoOne thing I love about OCaml is named parameters. I am an explicit kind of guy and ironically even with all the type inferencing OCaml has it is damn explicit of a language (although I sort of prefer ad-hoc poly but oh well). It seems like a pretty trivial thing to have named parameters but there are so many languages that do not have this feature (or have this feature and it breaks something (scala->java)).
- Tomte 11y agoBut still no Windows support for OPAM, right? sigh That's one of the things that Rust absolutely got right, helping its adoption tremendously.
- e_d_g_a_r 11y agoYou could turn that sigh into a PR. You want it, then make it happen, this is open source after all. Here is something that might help in the mean time, https://www.typerex.org/ocpwin.html https://www.typerex.org/ocpwin.html.
- Tomte 11y agoThat's a stupid reply. I don't question the right of the Ocaml/OPIE developers to ignore platforms they don't like and use. I question the wisdom in doing so. Asking people who aren't invested in the slightest in your language so far to start hacking on your tooling before they can try and learn your language is an interesting onboarding strategy.
- e_d_g_a_r 11y agoNo one has a vested interest in your interests other than you.
- lpw25 11y agoIt's being very actively worked on. No solid ETA yet but I would expect the coming version or the one after to work out of the box.
- mands 11y agoGreat talk - for me the module system, and functors in particular, are one of the great strengths of OCaml. I'm hoping increases usage by Jane Street, Facebook and Bloomberg, along with the Unikernels/Docker tie-up will lead to increased uptake and visibility. I personally find it more suitable to systems space than Go, but with far more features to help build correct code. (shameless plug - we're using OCaml for our microservices platform in London and are now hiring - https://www.stackhut.com/#/careers https://www.stackhut.com/#/careers)
- hellofunk 11y ago"Raise your hands if you know who John Carmack is." (long pause) "Raise your hands if you've ever played Quake, or Rage, or Doom..." (pause) "...or if you've ever heard of them." (pause, blank stares) "Oh! Well, I guess it's been awhile..."