10 ms·
Anyone 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 c
by melling 13y ago
Anyone 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?