Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I too dislike tutorials focused around the REPL, because it's rarely how you write code in practice. The REPL is more of a debugging tool for quickly testing and manipulating code you've already written.

I'd jump directly into creating code files/modules - begin by creating simple programs using `main` as you would in other languages. Create a file with ".hs" extension, (Haskell filenames start with capital letters by convention, since they share the name of the module)

    main :: IO ()
    main = putStrLn "Hello World!"
Make sure the GHC bianries are in your PATH, and use the program "runhaskell File.hs" to quickly compile and execute them. Also, from ghci you can use the command (:load File.hs), if you want to debug stuff.

The preferred tool for writing Haskell though is emacs. If you're not familiar with emacs, its also a great opportunity to learn. Grab emacs 24, and open the file ~/.emacs, paste in this snippet:

    (require 'package)
    (add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/"))
    (package-initialize)
Then hit M-x (M for Meta, which is typically Alt), and type eval-buffer into the minibuffer at the bottom. If no errors, hit M-x and type into the mini-buffer:

    package-install haskell-mode
You can add the following to the .emacs file and use `eval-buffer` again. Also save with C-x C-s.

    (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
    (add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
Next step is to just open an empty haskell file. Create a new directory for your project with `M-x make-directory`, and create a new haskell file using C-x C-f. Add some code, and hit C-c C-l (load in ghci), and emacs will pop open a new window, launch ghci and load your code into it.

You can switch over to the ghci window with C-x o (cycle windows), and type some code into it for testing. (Note it can be quite buggy if the cursor is not in the right place - there needs to be a space after the prompt > ¦). Useful keyboard shortcuts to remember here are M-p (previous) and M-n to cycle through previously used commands. (You can rebind any keys to how you wish.)

You can also add typicall IDE features like "run without debugging" and such with simple snippets of elisp. Say if we want to bind F6 to this command, we can add this hack to .emacs, save and eval the expression.

    (global-set-key [f6] 
        (lambda () 
            (interactive) 
            (async-shell-command (concat "runhaskell " buffer-file-name))))
Now with an active haskell file, F6 will launch like an application, bypassing GHCI - and emacs will typically open a new window (or replace an existing one) with the stdout/stderror for the application.

That'll get you started anyway. From there, using LYAH is a good beginner resource, although it won't get you much further than past the syntax and semantics of the core language. It doesn't cover the plethora of language extensions, building projects, using cabal, haddock, quickcheck and more. I'm not aware of a single resource which covers these though - I just learned what was needed as and when by searching - the best information typically comes from one-off blogs focused on a specific problem.



> I too dislike tutorials focused around the REPL, because it's rarely how you write code in practice....

> The preferred tool for writing Haskell though is emacs...

I understand, but if your goal is to pull in new Haskell programmers you want to grab them as quickly as possible. You want them to try something immediately, not force them to invest in a toolset from the get-go.

I wasn't interested in Haskell until I used the REPL. It allowed me to get immediate feedback on how Haskell works. The tutorial got me engaged quickly.


> The preferred tool for writing Haskell though is emacs.

That's news to me. Emacs is a popular editor for Haskell and in general, but I don't see any reason to say it's "preferred". Unlike, say, as it is with Agda or Coq. I use Sublime Text and Atom mostly (although occasionally emacs), and it's perfectly happy. Then again, all I really ask for from an editor is syntax highlighting, basic tab completion and sensible automatic indentation. But for those who want more, there's the SublimeHaskell plugin, although I've had mixed results with it.


Thanks for the help, though Vim is already my choice for insanely-powerful text editor with 2k+ page user manual :) I tried emacs, but the dependence on the control key for everything makes it kinda painful on every keyboard I have. I suppose I could probably remap a bunch of keys or something, but Vim seems to work much better in the default configuration. I know this tends to be a big flamewar subject, so it is what it is.

Anyways, your Hello World and runhaskell are much more along the lines of what I'm looking for. Also checking out the other people's suggested Learn you a Haskell.


> Also checking out the other people's suggested Learn you a Haskell.

Just to get the minority opinion out there, I found the style of Learn You a Haskell to be insufferable. Real World Haskell is available free online[1] and has a straightforward tone of voice.

[1] http://book.realworldhaskell.org/


I found Learn You a Haskell to be one of the best programming books I've ever read and no I'm not joking. It's a work of art.


I've just peeked at this book again, and I noticed that they do start with ghci. They're very up-front about its peculiarities, though.


Well, to each their own. I just finished the third chapter last night, and I'm enjoying it so far.


So, write the same thing but in Vim macros...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: