7 ms·
Haskell features I'd like to see in other languages
- bad_user 16y agoWow ... I just saw type-safe macros / quasi-quoting. It seems that Haskell is the language I've been searching for all my life :-) ... unfortunately code written in it looks really scary. I mean ... when I look at good Python / Ruby code, I just get what's going on ... that's partly because I know these languages, but on the other hand they encourage programmers to do literary story-telling. Maybe someday Haskell will get reinvented into some form meant for mere mortals :)
- eru 16y agoI really like the syntax of Haskell. You can get used to it fairly quickly. > [...] but on the other hand they encourage programmers to do literary story-telling. Try literate Haskell. (I say this only partially tongue-in-cheek.) Really, I think that Haskell is very geared towards being read by humans. This is mostly because it's so good at gluing, that you can break your problem apart in smaller pieces in the first place. E.g. let's look at a program that computes sqrt(2). With Haskell you can talk first about how to produce infinite list of ever better approximations: next x = (2 / x + x) / 2 approximations = iterate next 1 and then talk about when to stop and finally give an answer later on. goodEnough x = abs (2-x*x) <= 0.0001 sqrtTwo = find goodEnough approximations In a strict language, you usually intermingle producing new values with watching out for the ending condition: guess = 1 while abs(2 - guess*guess) >= 0.0001: guess = (2/guess + guess) / 2 Python's generators help, but they are more restricted. (Which is intended, Python wants to stay simple.)
- JadeNB 16y agoJust out of curiosity, what's the advantage of `(2/x + x)/2` over `x/2 + 1/x` (which, to me, is more readable)?
- eru 16y agoIt is more readable. But I wanted to stick close to the general formula [1], so that you see what's going on. It would probably have been better to write it as: y = 2 average x1 x2 = (x1 + x2) / 2 next x = average (y/x + x) It's just a co-incidence that I chose y as 2. [1] Heron's Method (http://en.wikipedia.org/wiki/Herons_method#Babylonian_method http://en.wikipedia.org/wiki/Herons_method#Babylonian_method)
- danieldk 16y agoI'd say: just try it. After reading haskell-cafe for a day, I felt utterly incapable of ever learning Haskell. I started picking up the language using Real World Haskell (quite an acceptable book). I can read most programs now and crafted some short programs. I still find predicting memory/time use under a lazy-evaluation regime difficult. But it is certainly a nice experience ;)
- eru 16y agoThe guys on Haskell Cafe delight in complex problems (and solutions). There's a list for beginners, too. You will enjoy the Haskell Cafe later. By the way, one small exercise (from a paper by Chris Okasaki): Write a function with the type Tree a -> Tree Int where data Tree a = Node a (Tree a) (Tree a) | Empty your function should all nodes in a tree with their position in level order traversal (also called breadth first search). E.g. (lot == lever order traversal) lot Empty = Empty lot (Node 'a' (Node 'b' Empty (Node 'c' Empty Empty)) (Node 'd' Empty Empty)) = (Node '0' (Node '1' Empty (Node '3' Empty Empty)) (Node '2' Empty Empty))
- gaius 16y agoThe thing with RWH I found was, chapters 1-4 great, a nice introduction to the language, and I did all the exercises without too much head-scratching. Then chapter 5 is utterly incomprehensible. I didn't get any further. OCaml, that's a nice language, with much better introductary materials.
- gtani 16y agoDont' give up: http://www.amazon.com/Programming-Haskell-Graham-Hutton/dp/0521692695/ http://www.amazon.com/Programming-Haskell-Graham-Hutton/dp/0... http://stackoverflow.com/questions/1012573/how-to-learn-haskell http://stackoverflow.com/questions/1012573/how-to-learn-hask... http://learnyouahaskell.com/ http://learnyouahaskell.com/
- anonymousDan 16y agoWhat did you find so hard about chapter 5 ("Writing a library: working with JSON data")? Doesn't seem too be such a big step up to me in comparison to other chapters in the book?
- loup-vaillant 16y ago> on the other hand they encourage programmers to do literary story-telling. That is the part you must avoid. In Haskell, you don't tell a story. You depict a landscape. (There is a story, in the IO monad, but it is minimized and segregated.)
- stonemetal 16y agoMy background is almost all C, C++, and some python. I have been fooling around with Haskell for a few months now. I can honestly say I haven't used anything on the list beyond the first two. Haskell falls very close to python in readability if you want it to. You can "go deep" and produce stuff that only an experienced, hard core functional programmer can understand as this guy does, but I tend to use it as a statically typed, fast python. Flip through the first few chapters of Real World Haskell online and see that it is fairly readable.
- edanm 16y agoAs a non-Haskell programmer, I didn't understand almost any of the examples. Some of them I understood, but didn't get what the feature is good for. Some of them I just plain didn't get (Haskell's syntax is to different from what I'm used to to easily understand what's going on). I love the idea of this article, talking about specific features in your language you'd like to see gain wider adoption. It's too bad I had trouble understand what was going on.
- eru 16y agoThat's a shame. And I agree. I guess the author should provide translations in C or Java or whatever along with his examples as far as possible. I understand his Haskell examples, but that's preaching to the choir. I read a blog post were somebody re-implemented some examples of Haskell concepts in C. I can't remember the URL, but searching for "Monads in C" or "Monads in Java" etc brings up some interesting posts. E.g. http://blog.tmorris.net/maybe-monad-in-java/ http://blog.tmorris.net/maybe-monad-in-java/
- intoverfl0w 16y ago(author here). This is good feedback. I definitely struggled with finding a good way to describe what each of the features does. I'm considering writing a sequence of followups, each focusing on a particular feature, trying to describe how (say) Java code could be refactored to take advantage of the feature. I'd very much appreciate suggestions for things to address in such a series! Any time I write something like this, I always learn a lot in the ensuing discussions. Mostly I learn technical things, but it's good to learn more about the writing process as well.
- edanm 16y agoHere's where I think the article goes wrong. Firstly, I'm not sure what audience you had in mind when you wrote this. If it's Haskell programmer, then I think it's too detailed. If not, I think it's not detailed enough (but it does include all sorts of words/concepts a non-Haskell programmer won't understand). I think a shorter article, focused on less features would have been better (or a series of articles). It feels to me like you could talk in length about each of these features, and the article is both too long because of so many features, but too short in each specific feature. Finally and most importantly, what you're really missing is code examples in other languages. You mentioned doing this, and I agree 100%. That would make your article much easier for someone like me. Although I vote the examples be in Python :) I'm looking forward to your next post!
- city41 16y agoI've had a casual relationship with functional languages for a while now. I've decided to really get into one and chose F# as there is a possibility of using it on the job. For the functional gurus, should I ignore that benefit and go to what seems to be the holy grail of functional languages, Haskell?
- deleted 16y ago[deleted]
- dons 16y agoYou work in a .NET environment, or have Windows lock-in? Otherwise, why not use Haskell on the job, like others do: http://haskell.org/haskellwiki/Haskell_in_industry http://haskell.org/haskellwiki/Haskell_in_industry
- city41 16y agoI work in a .NET environment. I am learning F# in my spare time with hopes of being able to write part of the app in F#. It may not be an entirely realistic hope, but at least that hope is there with F#.
- intoverfl0w 16y agoIf you work in a .Net environment and want to explore the possibility of using functional programming on the job, F# is definitely a natural way to go. Depending on how much autonomy you have at work, Haskell is a reasonable language for getting "real work" done. The technology is ready to do the job: as with any language selection discussion, the issue is more about workplace culture (maintenance plans, hiring, etc). In terms of advanced types, my understanding is that F# is playing catch-up to Haskell, but that's not really a slight against F#. I happen to prefer Haskell, but it's not obvious to me that a person who knows both languages would agree with me. So the answer your question, or more precisely to not answer it, I think you'll find either language rewarding. If you think F# might be more useful at work, that's definitely worth considering. If you want to see the state of the art in (well supported) statically typed functional programming, Haskell is a strong contender. Regardless of which you decide to look at, the skills will translate fairly well if you later go to look at the other.