views:

759

answers:

6

I am a bit rusty on my Haskell and am looking to ramp back up. One thing I enjoy from F# is the F# Interactive shell integrated with Visual Studio: I can evaluate virtually anything (including function and class definitions) and use F# as a shell. Is there an equivalent in Haskell? When I use ghci, I cannot evaluate function definitions. How do you work around that?

My current preferred setting is using Emacs with haskell-mode and opening an interactive ghi interpreter. However, is there a way to evaluate just region of a file?

+2  A: 

You can define a function using 'let':

$ ghci
Prelude> let double n = n + n
Prelude> double 42
84

Also, I won't quite recommend this, since (A) I wrote it, and (B) it's terribly undeveloped, but Halp can be handy in Emacs -- it's a little bit like a spreadsheet for Haskell code integrated right into your source-code buffer. You can have a set of expressions you're interested in and with one keystroke see how all their values change depending on your edits since the last reevaluation.

Darius Bacon
Thanks. Halp looks indeed handy. I'll be trying it out :)
namin
Cool! Improvements would be welcome -- the Python version is more polished because I use Python more.
Darius Bacon
+1  A: 

ghci provides some special syntax for doing things like that. Use let for function definitions. However, it's noted on the GHC FAQ that it can be tricky with complex functions and generally loading from a file is preferred. If you use something like Emacs that allows for easy integration between your source file and a ghci process, it becomes a non-issue because you can load your definitions into the interpreter with a keystroke.

J Cooper
In Emacs, is there a way to load a region of a file as opposed to the entire file?
namin
No, here are the haskell-mode ghci commands:haskell-ghci-load-file haskell-ghci-modehaskell-ghci-reload-file haskell-ghci-show-ghci-bufferhaskell-ghci-start-processYou just get load/reload support.
Nathan Sanders
A: 

With O'Caml and F#, definitions proceed from the top of the file down. However, in Haskell, a type or function defined earlier in a file can reference a type or function later in that file. So in Haskell, a module needs to be compiled all at once. An interactive loop is incompatible with compilation all-at-once.

Justice
A: 

You might also check out Visual Haskell (for VS 2003 or 2005) if you enjoy F#'s integration into the VS IDE.

http://www.haskell.org/visualhaskell/

Jared Updike
It doesn't seem to be actively maintained.
namin
Probably true. As I recall the developer volunteering didn't have all the necessary versions of VS to test it on...
Jared Updike
+1  A: 

Haskell mode for Emacs provide functions for work with intepreter. So if you want to run some function from the module that you edit, you can use C-c C-l to load current module to ghci (or another haskell interactive) and switch to buffer with interactive and execute some commands there

Alex Ott
+1  A: 

As you've observed, GHCi is fairly limited in what it can do --- short functions or expressions are about all that's easy at the command line. For the rest I find myself loading modules. One of the difficulties is that Haskell does not have definition before use so it is often hard to pull a meaningful fragment out of a file. I generally find myself defining small modules and loading and reloading them. The interactive experience is definitely not as nice as one might like.

Norman Ramsey