17
votes

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.

3
How are you launching ghci? Are you running ghci from the command line or some other method?Gabriella Gonzalez
There is a link in my start menu that I click. Does that answer your question?Stan Shunpike
I don't use Windows, but :!cd should give you the current directory, and :cd <dir> should change directory to <dir>.jub0bs
If you're using stack, you could simply run stack ghci.SwiftsNamesake

3 Answers

26
votes

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
1
votes

well just answer is :cd as @Zeta wroted but futher more, for searching someone like me

  • path: :cd
  • path with command: :! cd just like vim ! is of user's OS console
  • :cd <DIR> is almost equor :! cd <DIR>
  • there's another way :show paths that shows somepaths, and there's current dicectory

PS> Searched some conf.d(~/.ghci/ghci.conf) like one but failed.

1
votes

What worked for me in Windows 10 with GHCi-8.10.4 is

:cd c:\\Work\Finance\UT Trial Balance

And

:cd ..

Works well, too. One assumes Haskell strings [Char] but no, literal strings. The spaces do not even have to be escaped. The Windows backward slash works, too. ugh. You still have to use

:! cd

to see where you are. I am assumming, now I can :l my code in my working directory.

If you go out with like :! bash -i it is a fork and separate. You can cd all over but it will not change the GHCi working directory.

And :! pwd works in my GHCi.

Now you get very many Linux tools when you install Haskell-dev. It's sweet. I got rid of Cygwin and installed Ubuntu 20.04.1 LTS in windows last year which is real Linux on Windows but the commands are not available at the Windows command line. Now, with Haskell-dev I get Linux everywhere for free. Joy.