Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set current directory in GHCi?

Tags:

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.

like image 764
Stan Shunpike Avatar asked Jul 03 '15 19:07

Stan Shunpike


People also ask

How do I change directory in Haskell?

:cd -- change to another directory. :type [expr] -- give the type of an expression (or -- just :t) :set +t tell Haskell to print type information after evaluation.

How do I run a program in ghci?

If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu. And you are presented with a prompt. The Haskell system now attentively awaits your input.

How do I load files into ghci?

From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.


2 Answers

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
like image 55
Zeta Avatar answered Nov 05 '22 05:11

Zeta


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.

like image 1
강준모 Avatar answered Nov 05 '22 06:11

강준모