10 ms·
A brief introduction to Haskell, and why it matters
- melling 13y agoAnyone have a practical guide to Haskell? For instance, how can I open a file, split a line on a separator, then do something on the third column? Also, how can I talk to MySql or Sqlite? Load a table then perform operations on the loaded data? I've found the best way to learn anything is to find real uses in my day to day work and start small. In short, find some tasks for which I would use Perl|Python|Ruby and solve them with Haskell.
- pdpi 13y agoYou're probably going to have a bad time trying to do it like that. Perl/Python/Ruby are brilliant at what they do, and the problems they're best at aren't necessarily problems that Haskell is very good at. If you're interested in learning the language, I suggest starting with Learn You a Haskell For Great Good[1] (first programming book I've ever considered to be a page-turner), then following it up with Real World Haskell[2] (going through that one now myself) [1] http://learnyouahaskell.com/ http://learnyouahaskell.com/ [2] http://book.realworldhaskell.org/read/ http://book.realworldhaskell.org/read/
- polymatter 13y agoTo expand on this, Haskell is excellent to make your own parser. Take special note of say Chapter 16 Parsec of Real World Haskell[1]. This is the sort of application where you would choose Haskell over Perl/Python/Ruby. Of course parsing is something thats very important if you are making your own DSL. And this is another application where Haskell really shines. This is why the guys who do programming language research produce papers with code in Haskell. Its orders of magnitude easier to make your toy language for exploring evaluation order in Haskell than it is to do the same in Java. Its easier to learn a language if you're building something you're interested in though. Personally, I learnt a lot of Haskell implementing a web app with Yesod[2]. Good luck. [1] http://book.realworldhaskell.org/read/using-parsec.html http://book.realworldhaskell.org/read/using-parsec.html [2] http://www.yesodweb.com/ http://www.yesodweb.com/
- jlarocco 13y agoI don't really agree. Python, at least, has the PLY library, which makes parsing (IMO) easier than with Parsec. Other languages, like OCaml, also make writing parsers very easy. Haskell with Parsec might beat C with lex/yacc, but I don't think it's that great compared to what else is available.
- tel 13y agoCould you elaborate why you feel this way? I could also just vote for one or the other, but I'd like to see discussion about the relative merits of PLY or Camlp4 w.r.t. Parsec.
- jlarocco 13y agoI can't deny part of the reason is that I'm simply more comfortable with Python. I think my main contention was the "orders of magnitude" claim in the comment I replied to. Parsec is nice, but it's not "orders of magnitude" better than PLY or camlp4 or other parsing tools in other languages.
- dangirsh 13y agoAfter using PLY in production and (afterwards) toying with Parsec for a side project, I could never go back to PLY. Even aside from PLY's obvious flaws (docstrings as code, mutation instead of returns, ample use of python magic backstage...), Parsec wins by being more configurabile and composable. Whoever is maintaining my PLY mess now might get a handle on it faster than they could learn Haskell, but probably not by much.
- gohrt 13y agoHaskell also has Attoparsec, a simpler faster alternative to Parsec
- kd0amg 13y agoOf course parsing is something thats very important if you are making your own DSL. And this is another application where Haskell really shines. This is why the guys who do programming language research produce papers with code in Haskell. Its orders of magnitude easier to make your toy language for exploring evaluation order in Haskell than it is to do the same in Java. I feel like I see a lot more cases where the research language is built as an extension of Haskell or its syntax tree is written up as a (G)ADT possibly with the derived `read' as the "parser" (but possibly just constructing and interpreting ASTs within a Haskell program).
- quchen 13y agoReal World Haskell (RWH) [1] is probably what you want, although it may not be as good of a first introduction as other books such as Learn You a Haskell (LYAH) [2] or Programming in Haskell [3]. [1] http://book.realworldhaskell.org/ http://book.realworldhaskell.org/ (free to read online) [2] http://learnyouahaskell.com/ http://learnyouahaskell.com/ (free to read online) [3] http://www.cs.nott.ac.uk/~gmh/book.html http://www.cs.nott.ac.uk/~gmh/book.html
- ajtulloch 13y agoIf you're looking for a guide to using Haskell in the real world, you should check out "Real World Haskell", by O'Sullivan et al, freely available online at [1]. It's an excellent book, in my opinion one of the best programming books available on any language. In particular, for interacting with MySQL, check out the chapter on that in RWH [2], and for reading lines and operating on them, see [3]. However, you should start from the beginning, as those kind of operations (for better or worse), require a firm grounding in Haskell concepts like types, monads, etc. [1]: http://book.realworldhaskell.org/read/ http://book.realworldhaskell.org/read/ [2]: http://book.realworldhaskell.org/read/using-databases.html http://book.realworldhaskell.org/read/using-databases.html [3]: http://book.realworldhaskell.org/read/io.html http://book.realworldhaskell.org/read/io.html
- robinh 13y agoRWH is a good recommendation, although it contains a rather large number of errors. LYAH is very good for learning the basics, too. But, if you're already a very experienced programmer, you can probably learn how to write practical programs in Haskell by just reading the IO chapter (http://book.realworldhaskell.org/read/io.html http://book.realworldhaskell.org/read/io.html) and the Systems Programming chapter (http://book.realworldhaskell.org/read/systems-programming-in-haskell.html http://book.realworldhaskell.org/read/systems-programming-in...) from RWH. These will help you understand how IO actually works in Haskell. The rest is just libraries and learning the language itself. On a related note: I've found that starting with the main IO function is a good way to start writing any large program in Haskell. Most people I know who complain about Haskell being a mess in impure environments tend to write pure functions first, then build their IO functions on top of that, instead of the other way around. I'm not sure whether this applies for everyone, and of course this approach works well in domains that have little to do with IO (e.g. mathematical programming), but it's something to keep in mind.
- dllthomas 13y ago"For instance, how can I open a file, split a line on a separator, then do something on the third column" Assuming we have doSomething :: Maybe String -> IO () which turns some optional string into an action to perform, you could say: mapM_ (doSomething . listToMaybe . drop 2 . words) . lines =<< readFile filename The IO action readFile produces a (lazy) String, lines chops it into a (lazy) list of Strings - on each of those we break it into columns, get rid of two of them, get Just the head if it exists (otherwise, Nothing), turn those into the actions we want with doSomething, and sequences those actions. The result is a new action. "Also, how can I talk to MySql or Sqlite?" http://www.yesodweb.com/book/persistent http://www.yesodweb.com/book/persistent
- asattarmd 13y agoBeginning Haskell: A project based approach is a good book. http://www.apress.com/9781430262503 http://www.apress.com/9781430262503
- gaius 13y agoHere's an example of SQLite http://gaiustech.wordpress.com/2010/09/19/scope-in-database-code-2/ http://gaiustech.wordpress.com/2010/09/19/scope-in-database-... And processing a logfile http://gaiustech.wordpress.com/2010/09/13/analyzing-logfiles-2/ http://gaiustech.wordpress.com/2010/09/13/analyzing-logfiles...
- maxerickson 13y agoRosetta code is sometimes reasonable for answering these questions: http://rosettacode.org/wiki/CSV_data_manipulation#Haskell http://rosettacode.org/wiki/CSV_data_manipulation#Haskell (early Google search result for 'rosetta code csv') edit: Looking at the python code, I would call the approach esoteric, but I guess that's just an opinion (I would use the csv module without giving any consideration to the quality of the structured file). So who knows how idiomatic the code is.
- codygman 13y agoRealize that the Haskell is not using an external csv library like the others are before making judgements ;)
- maxerickson 13y agoA lot of them don't. I like the idea, but it's weird to not show the straightforward method of dealing with a csv. Some other toy problem would demonstrate opening files and a little parsing.
- tel 13y agoOthers have recommended good books, but here's a glimpse just immediately. import Control.Monad (forM_) import Data.List.Split (splitOn) import Data.List (transpose) import System.IO main :: IO () main = do withFile "file.file" ReadMode $ \handle -> contents <- hGetContents handle let clines = lines contents ccols = transpose (map (splitOn ",") lines) forM_ (ccols !! 3) $ \cell -> do putStrLn cell Then take a look at the following packages http://hackage.haskell.org/package/split http://hackage.haskell.org/package/mysql-simple http://hackage.haskell.org/package/sqlite-simple
- platz 13y agoI think this is the corrected version import Control.Monad (forM_) import Data.List.Split (splitOn) import Data.List (transpose) import System.IO main :: IO () main = do withFile "tel.csv" ReadMode $ \handle -> do contents <- hGetContents handle let clines = lines contents ccols = transpose (map (splitOn ",") clines) forM_ (ccols !! 3) $ \cell -> do putStrLn cell
- tel 13y agoGood catch—obviously I wrote that freehand :)
- mightybyte 13y agoHaskell can do these practical things quite easily. import Data.Char changeThird (a:b:c:rest) = a : b : map toUpper c : rest main = do contents <- readFile "foo.txt" let ls = lines contents ws = map (changeThird . words) ls writeFile "foo2.txt" (unlines $ map unwords ws) But like others said, I think you're going to have a harder time if you try to learn Haskell by starting this way. Anyone learning Haskell will be coming in with no exposure to any other pure language, and that has big implications. There is a fundamental difference between the "contents <- readFile ..." line above and the next two lines that you have never encountered before in any other language. You'll probably have a pretty difficult time with it if you don't understand a little more about how the readFile function is different from the lines function and what that means for how you can use them. You can't just mindlessly copy examples from small day to day tasks like you can with other languages because Haskell is unlike any other language you know. Please don't read the above paragraph to mean that Haskell is not suitable for these kinds of small tasks. I actually think that Haskell makes a fantastic scripting language. I'm just saying that you can't learn it like you learn other scripting languages. You need to get a bigger understanding of what's going on otherwise you'll end up frustrated about why things don't work the way you expect. All that being said, I still HIGHLY recommend learning Haskell.
- prakashk 13y agoI recently learned about the `interact` function in Haskell, which makes it easy to write programs reading from standard input and write to standard output, just like many of the Unix tools. For example, if you want to print the id and name of each user from a file in the /etc/passwd format: -- display all users: ids and names from /etc/passwd import Data.List.Split (splitOn) eachLine :: (String -> String) -> (String -> String) eachLine f = unlines . map f . lines showUser :: String -> String showUser line = fields !! 0 ++ "\t" ++ fields !! 4 where fields = splitOn ":" line main = interact (eachLine showUser) This can be run, like (assuming the program is saved in list-users.hs): $ runhaskell list-users.hs < /etc/passwd I am very new to Haskell, and I am sure there are other, perhaps better ways to do this.
- Tyr42 13y agoIt looks ok to me, though I might pattern match in the `where fields` part instead of using `!!`. Still looks pretty good. If I was feeling fancy, that `unlines . map f . lines` looks like an isomorphism that we could do something awesome with the lens library. Anyone who knows it better care to show me how to do that?
- pdpi 13y ago> Haskell is in some ways a nicer language for functional programming than Scala, and if you are serious about learning more FP, we recommend learning it. I'd take it as far as to say that, if you're a developer of any sort, you should learn at least some Haskell at some point. It does things in such a completely different way from more mainstream languages that the sheer mind-opening effect of learning it is worthwhile.
- JimmyM 13y agoI have about a half a year's worth of Racket (acquired over around a year and half) under my belt, and intend to learn Haskell in the coming two years. Is there anything startlingly different in Haskell compared to the LisPs? EDIT: Thank you for the responses, I have bookmarked the relevant github page and feel like my question has been well-answered :) Also, even more interested by Haskell than I was before.
- dllthomas 13y ago"Is there anything startlingly different in Haskell compared to the LisPs?" Static types, possibly more purity (depending on the lisp you've been writing), pervasive laziness, slightly awkward (but usually unnecessary) macros.
- taeric 13y agoI'm noticing a distinct lack of "productivity." :)
- Kiro 13y agoI'm a JS and PHP developer who wants to learn Haskell. I would prefer to build something rather than just doing a tutorial or reading a book. Do you have any recommendations on projects suitable for a beginner?
- jasonwocky 13y agoMy step 1 to understanding a new language has always been to build a simple, file-serving HTTP server without using any HTTP-specific libraries.
- arms 13y agoI really like this suggestion. I'm in the process of learning Haskell now, and plan on using this as a practical learning exercise. Out of curiosity, how difficult did you find this to complete?
- codygman 13y agoI think this is perfect for you (and many others): http://www.amazon.com/Beginning-Haskell-A-Project-Based-Approach/dp/1430262508 http://www.amazon.com/Beginning-Haskell-A-Project-Based-Appr...
- general_failure 13y agoCan someone give examples of successful/popular Haskell programs?
- tel 13y agoxmonad, darcs, pandoc are three big ones
- peteretep 13y agopandoc is a beauty
- pmahoney 13y agoHere's a list compiled by someone else: http://haskell-news.blogspot.com/2008/01/top-10-most-popular-haskell-programs.html http://haskell-news.blogspot.com/2008/01/top-10-most-popular... Here's a GitHub search. Mostly libraries, but a few end-user programs: https://github.com/search?p=1&q=language%3Ahaskell+stars%3A%3E200&ref=searchresults&type=Repositories https://github.com/search?p=1&q=language%3Ahaskell+stars%3A%... I didn't realize Pandoc was written in Haskell. Darcs of course, and xmonad window manager are the ones that come to my mind.
- boothead 13y agohttp://www.ledger-cli.org/ http://www.ledger-cli.org/ has a port in Haskell (http://hledger.org/ http://hledger.org/ that from what I've heard will be the main branch in future.
- boothead 13y agoHaskell also matters from a business point of view! I can tell you that hiring Haskellers immediately connects you with the top of the food chain in terms of hiring.
- boothead 13y agoActually I thought of another reason why Haskell matters. My HN score has gone up over 60 points in the last few days from only 4 comments here about Haskell. It's really a very efficient tool to have in your arsenal :-)
- namelezz 13y agoCan you give me a list of companies that are looking for Haskllers?
- mightybyte 13y agoI don't know about actively looking, but I know that the following companies use Haskell. Janrain (http://janrain.com/ http://janrain.com/) Standard Chartered Bank (https://www.sc.com/en/index.html https://www.sc.com/en/index.html) Karamaan Group (http://karamaan.com/ http://karamaan.com/) Silk (http://www.silk.co/ http://www.silk.co/) Skedge.me (http://skedge.me/ http://skedge.me/) Soostone (http://www.soostone.com/ http://www.soostone.com/) Erudify (http://www.erudify.com/ http://www.erudify.com/) Facebook (http://www.haskellcast.com/episode/004-simon-marlow-on-parallelism-and-concurrency/ http://www.haskellcast.com/episode/004-simon-marlow-on-paral...)
- boothead 13y agoI'm CTO of a team of Haskellers in London. We're not hiring at the moment, but if you ping me on ben <at> dlstartup dot com I'll keep your CV around. Otherwise there are 5-6 haskell startups and probably others on the finance world. Whereabouts are you? In addition to mightybyte's list there's also http://www.borde.rs/ http://www.borde.rs/ Barclays Capital http://www.sqreamtech.com/ http://www.sqreamtech.com/ Tsuru
- 13y ago
- jbeja 13y agoWhy don't consider Clojure as an available option to Haskell or Scala?
- namelezz 13y ago.syntax LISP with comfortable is everyone Not
- metaphorm 13y agoLISP is prefix notation, not just reversed order. An expression should begin with a function (a verb) and be followed by a list of its arguments (the nouns). (not (comfortable everyone LISP-syntax))
- dllthomas 13y agoI'd expect the quantifier to take the predicate as an argument, rather than the other way around. (not (everyone comfortable LISP-syntax))
- klrr 13y agoWhile Clojure does a lot of things well, it have the same flaws that Haskell (and to some extend Scala) tries to solve. For example, Haskell has strong emphasis on types and has no side-effects which let you easier write correct programs. One beautiful thing you'll encounter doing after some experience with Haskell is that types will guide you when designing your programs, not only giving a correct but also concise and elegant solution (most of the times).
- dschiptsov 13y agoMuch more carefully written no-nonsense overview http://en.wikipedia.org/wiki/Haskell_features http://en.wikipedia.org/wiki/Haskell_features
- erjiang 13y agoDon't forget Hoogle[1], the type-based search engine! Let's say that I want to combine a couple of my lists into one list. I know what my inputs look like, and I know what I want my output to look like. So I go to Hoogle, and search for "[a] -> [b] -> [(a, b)]". First result is "zip". Or I want to get only the things in a list that satisfy a predicate, but I'm suffering from nominal amnesia. I type in "(a -> Bool) -> [a] -> [a]" and Hoogle lets me know that I meant to write "filter". [1] http://www.haskell.org/hoogle/?hoogle=%28a+-%3E+Bool%29+-%3E+[a]+-%3E+[a] http://www.haskell.org/hoogle/?hoogle=%28a+-%3E+Bool%29+-%3E...
- garretraziel 13y agoSqueak Smalltalk is better in it - you can actually type in arguments of method and a result and it can find you method that, given these arguments, returns this result. So, for example you can type: 'hello'. 'HELLO' and it finds you 'asUppercase' method of String object.
- pkinsky 13y agoHow does it perform this search? I can't imagine running all functions with a matching type signature scaling to more complex examples.
- kencausey 13y agoWell, in fact it does something like this. But what messages it tries is actually limited. It has been a while since I looked at it and so don't remember the details. This is a very useful function of Squeak and I presume Pharo Smalltalk but I think users of it should have some awareness that it has limitations and not to simply assume that because a result is not found that it does not exist.
- deleted 13y ago[deleted]
- misnome 13y agoThe problem with learning Haskell is I just can't shake the "Research" feeling. Learning Erlang looks a harder slog, but with the benefit of a "Real world" feeling, whether correct or not.
- peteretep 13y agoI have separately spent a week learning Erlang and learning Haskell. Erlang: much much easier to learn. Within a week I felt I could probably have built pretty much whatever I wanted. A few pitfalls, but it's predictable, and with very few difficult to understand parts. Also the OTP is almost CPAN-like in its ability to get you solving problems very quickly. HOWEVER: until the next time I need to build a telephony switch or any kind of massive message handler (chat application, messaging queue, WhatsApp competitor etc), I just have no use for it. Haskell ... I had some basics in a week. After some months of dabbling here and there, I finally get Monads, but haven't done anything productive yet. I feel like I have become a significantly stronger developer in the other languages I use, have a much deeper understanding of some basics of CompSci, and have had a lot of fun. I think in another few months, it might start being my goto language for solving problems.
- Dewie 13y agoIt's funny to me how 'research' has become a put down. aka "research toy" vs "getting REAL work done".
- gohrt 13y agoIt's not a put down, it's an application area: Research: generate ideas Applied: deploy to thousands or millions of users
- codygman 13y agoMost of the time it's because those people can't make the connection between the research and the "real world". The worlds are very blurred in my experience.
- jayvanguard 13y agoThe language that has more lines of tutorials and articles written about it that actual lines of code.
- stewbrew 13y agoNow if somebody could tell me how to install the latest haskell platform on Ubuntu 12.04 and why cabal (with the haskell platform for 12.04) always gives me those incompatibility warnings, I'd be happy to try it out.
- deleted 13y ago[deleted]
- progman 13y agoHaskell's power is impressive but the "Cabal hell" is a serious problem. Recently I tried to get into Haskell. I've installed EclipseFP and the first sessions looked nice. But when I started to use external cabal modules I always ran into serious trouble with versioning. Even recompilation doesn't help a lot because there is always a complaining module. Do I something wrong? I am thankful for advice.
- m0a0t0 13y agoYou may want to try cabal sandboxes. I think it's a similar idea to virtualenv in python
- progman 13y agoThanks, I will take a closer look at it. I hope this is a hierarchical solution so that I won't need to download the whole cabal stack for any new project.
- codygman 13y agoIt is, you won't have to download the entire stack. You might want to try this tutorial out: http://yannesposito.com/Scratch/en/blog/Holy-Haskell-Starter/ http://yannesposito.com/Scratch/en/blog/Holy-Haskell-Starter...
- gohrt 13y agohttp://fpcomplete.com http://fpcomplete.com free Community edition online dev environment.
- drblast 13y agoOne cool thing about Haskell that really struck me and that usually isn't emphasized is partial application of function arguments. http://www.haskell.org/haskellwiki/Partial_application http://www.haskell.org/haskellwiki/Partial_application I'm not sure if it's unique to Haskell, but it's such a great feature that I've wished for in other languages since learning about it. You can replicate it by manually writing curried functions, but having the language do that for you is incredibly useful.
- Dewie 13y agoYou get this kind of thing "for free" in a concatenative language, e.g. a stack based language. Though having to mostly rely on stack-combinators (this depends on the language and/or idioms of that language, not something that is inherent in concatenative languages) for writing functions might feel too limiting.
- ExpiredLink 13y agomain :: IO () main = putStr "Hello world!!" A side-effect in the very first program. I'm disappointed!
- tsax 13y agoSo it fits what ESR says about Lisp? "Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot." Is that right? As Haskell seems to be far less intimidating to approach than Lisp, I may actually end up doing some. I fondly remember my junior level programming languages class and doing assignments in Standard ML. Haskell looks similar.
- dllthomas 13y agoMost people find Haskell (especially the laziness, the type system and its error messages, and the typeclass hierarchy) more intimidating than Lisp. If you're familiar with Standard ML, you already basically know the syntax, though, and the types and pattern matching will make you feel more at home.
- catilac 13y agoI've been wanting to make time to learn Haskell. But I have this problem, when learning any language, where I don't know what to build, but want to take a learn by doing approach. What are some good things to build when getting into a new language? Data structures? Chat server?
- jberryman 13y agoI really love writing libraries. Haskell has a type system that supports defining really solid abstractions (I.e. code that doesn't even permit wrong usage). Creating APIs like that is extremely satisfying. Maybe take a look on hackage and see if there's something missing that you'd like to see and make it.
- brlewis 13y agoWhat's with using "strict" for the opposite of lazy evaluation? Has the term "eager" fallen out of fashion? Strict seems overused to me.
- pekk 13y agoIt doesn't really matter, it's yet another language that can be used, that some people like and in this case really want to shove down everyone's throats. I guess it's not getting enough uptake organically, so once more to HN for the throat-shoving.
- codygman 13y agoName one language that got popular with no one posting/talking about it.