From HaskellWiki
Revision as of 07:50, 25 July 2006 by Simonpj (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Using GHCi

This page is a place to collect advice about how to use GHC's interactive interpreter, GHCi. Please add to it!

Using :def

The :def command, documented here, allows quite GHCi's commands to be extended in quite a powerful way.

Here is one example.

  Prelude> let loop = do { l <- getLine; if l == "\^D" then return () else do appendFile "foo.hs" (l++"\n"); loop }
  Prelude> :def pasteCode (\_ -> loop >> return ":load foo.hs")

This defines a new command :pasteCode, which allows you to paste Haskell code diretly into GHCi. You type the command :pasteCode, followed by the code you want, followed by ^D, followed (unfortunately) by enter, and your code is executed. Thus:

  Prelude> :pasteCode
  x = 42
  Compiling Main             ( foo.hs, interpreted )
  Ok, modules loaded: Main.
  *Main> x