In short, find some tasks for which I would use Perl|Python|Ruby and solve them with Haskell.
In short, find some tasks for which I would use Perl|Python|Ruby and solve them with Haskell.
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.
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)
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 [2] http://www.yesodweb.com/
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.
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.
Whoever is maintaining my PLY mess now might get a handle on it faster than they could learn Haskell, but probably not by much.
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).
Structural pattern matching is also a nice thing to have when writing interpreters.
Writing a DSL was my original motivation for learning Haskell beyond it being just a toy. About a day after I really got going on my parser I decided to abandon the idea of a DSL and just write it as a monad.
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) and the Systems Programming chapter (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.
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?"
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 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[1] http://book.realworldhaskell.org/ (free to read online)
[2] http://learnyouahaskell.com/ (free to read online)
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.
And processing a logfile http://gaiustech.wordpress.com/2010/09/13/analyzing-logfiles...
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/
[2]: http://book.realworldhaskell.org/read/using-databases.html
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.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?