Haskell – How to set current directory in GHCi

haskell

I am new to Haskell and using a Windows PC. I am trying to set up my GHCi interface so that I can write my code in my text editor before executing it using the GHCi.

Currently, my GHCi reads

$ ghci GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help 
Loading package ghc-prim ... linking ... done. 
Loading package integer-gmp ... linking ... done. 
Loading package base ... linking ... done.
Prelude>

According to this site, I have to save my Haskell files to the current directory or specify some other directory in order to access them. This is what I don't know how to do.

My Questions:

  1. How do I set the current directory?
  2. How do I get the GHCi to tell me what the path for current directory is so I can check what it is set at when I want to?

Please explain starting from the line

Prelude>

as noted above so that I can follow along.

Note:

The example Haskell code given was

file name: Main.hs

main = print(fac(20))

fac 0 = 1
fac n = n * fac(n-1) 

and in the GHCi

prelude> :load Main 
Compiling Main ( Main.hs, interpreted ) 
Ok, modules loaded: Main. 
*Main> fac 17 
355687428096000

So I want to save Main.hs to a directory, specify this as the current directory in the GHCi, and then run the above code.

Best Answer

How do I set the current directory?

GHCi provides the :cd <dir> command. You can get the list of all commands with :?. If you omit the directory, you will change to your home again.

How do I get the GHCi to tell me what the path for current directory is so I can check what it is set at when I want to?

Funny enough, GHCi doesn't provide a command for this, but you can use Window's cd command. In order to execute an external command, you need to use :!, e.g. :! cd.

Example

ghci> :! cd
C:\Users\Zeta
ghci> :cd workspace
ghci> :cd stackoverflow
ghci> :! cd
C:\Users\Zeta\workspace\stackoverflow
ghci> :cd
ghci> :!cd
C:\Users\Zeta
Related Topic