To call a command in the ghci automatically when it starts [duplicate] - haskell

This question already has answers here:
How to configure GHCi to automatically import modules
(2 answers)
Closed 8 years ago.
Every time I run the ghci at first I call the :set prompt "ghci> " command manually. Can it happen automatically, instead of manually?

Define prompt in one of the config files
Simply put that GHCi command in the appropriate GHCi config file (e.g. in your user-level GHCi config file $HOME/.ghci on Unix systems):
:set prompt "ghci> "
Create that file if it doesn't already exist. Then you won't have to run the command manually every time after starting GHCi. More detail about GHCi config files is available here.
By the way, another fashionable prompt is λ>.
Don't forget to also customize prompt-cont (continuation prompt)
As pointed out by kqr in his comment, if you activate GHCi's multiline input mode (:set +m), you may also want to redefine, for consistency, prompt-cont—or prompt2 prior to v8.2.1, as pointed out in this comment—which corresponds to the continuation prompt. So your .ghci file should contain something like the following two lines:
:set prompt "λ> "
:set prompt-cont "λ| "
Otherwise, the default continuation prompt (Prelude|) will be used.
Test in GHCi
λ> :set +m
λ> let fact 0 = 1
λ| fact n = n * fact (n - 1)
λ|
(0.01 secs, 1547336 bytes)
λ> fact 5
120

Related

Setting the ghci prompt with colors

I try to use terminal colors within my ghci prompt.
So when I open ghci and try to:
Prelude> :set prompt '\[\033[1haskell > \033[0m\]'
'\[\033[1\]haskell> \[\033[0m\] '
I know that these codes are interpreted by bash with echo and the -e flag. But how can I do this within ghci?
According to https://wiki.haskell.org/GHCi_in_colour, you can use
:set prompt "\ESC[33m\STXhaskell > \ESC[m\STX"
A few notes of explanation:
Only a double-quoted string is treated specially; single quotes are treated as part of the prompt.
The double-quoted string follows Haskell practice.
\STX corresponds to the \] of your bash prompt; it's not clear why GHCi does not require the equivalent of \[ as well. (Perhaps it does; I haven't played with this much.). See http://trac.haskell.org/haskeline/wiki/ControlSequencesInPrompt for an explanation.
Haskell character escape codes are in decimal:
Prelude> :set prompt "\027[31mhaskell>\027[0m "
Putting the same directive in your .ghci file should also work.

how to use ghci online- *h e l p*; it just scrolls fast?

when I tried to save the current ghci session using :save command I got the error
Prelude> :save
unknown command ':save'
use :? for help.
Prelude>
then when i type :? I got a ton of output that zoomed past the screen; how to read it one page at a time? is there anything like Unix less command there in ghci?
You could run GHCi from your shell and use shell redirection. This works for me on Windows:
echo :? | ghci > help.txt
more help.txt
I would expect this to also work on Unix, although I can't actually try it.

Calling Haskell script on mac?

I've installed the Haskell platform on my mac (OSX lion), and ghci is running great.
Now I've created a haskell-file, stored on my "desk." How can I call it from this directory?
Example:
Prelude> :load datei.hs
[1 of 1] Compiling Main ( datei.hs, interpreted )
datei.hs:1:7: parse error on input `\'
Failed, modules loaded: none.
datei.hs:
let fac n = if n == 0 then 1 else n * fac (n-1)
Why do I get this?
Use the OSX terminal to reach your desktop and invoke yourfile.hs using ghci:
cd ~/Desktop
ghci yourfile.hs
edit:
As stated in the comments, the error message you're seeing above is warning you that the character \ exists at an unexpected location in the source code.
Since that character does not exist in the line of code you posted, there must be more to datei.hs. We need to see the rest of your source code before we can help.
If you saved your program with TextEdit, it's very possible that you're seeing a '\' character because you're saving it as an RTF file (TextEdit's default). Hit Ctrl-shift-t to convert it into a plain text file.
If your already in ghci you can use ':cd /path/to/file' as well.
Here is a good thread discussing let.

How do you compile and run haskell on notepad++

How do you compile and run Haskell on notepad++
I installed the plugin NppExec and then I pressed F6
I saved my Haskell file to C:\Users\Sam\Desktop\haskell files\new 3.hs
So on the command after I press F6 I tried typing in a few different things:
C:\Users\Sam\Desktop\haskell files\new 3.hs`
ghc.exe new 3.hs
haskell new
but I got these responses:
C:\Users\Sam\Desktop\haskell files\new 3.hs
CreateProcess() failed with error code 2:
The system cannot find the file specified.
ghc.exe new 3.hs
Process started >>>
target `new' is not a module name or a source file
<<< Process finished.
haskell new 3
CreateProcess() failed with error code 2:
The system cannot find the file specified.
================ READY ================
What is the correct way of compiling and executing at haskell file on notepad++?
You need to set NppExec to work in the current directory, so In Plugins, NppExec, tick Follow $(CURRENT_DIRECTORY).
Use the command ghc new3.hs when you press F6 (no spaces in filenames).
If you're using Haskell with stack, I found a lovely way to run things quickly using NppExec. It's a simpler process than it looks and once you do it, you're good to go:
Suppose you have a file like this in a file called yourFileName.hs:
main :: IO ()
main = putStrLn "Hello world!"
Press F6 to begin NppExec. (See Note 1 below.)
Paste the below into the window.
cd "$(FULL_CURRENT_PATH)"
stack ghci
// This is a comment you can delete. Note 2 below.
(See Note 3 below.)
Upon pressing the OK button, the Notepad++ console will run the Haskell interpreter.
Now, press F6 again. A warning menu will pop up.
Type this into the menu: :cmd return $ unlines [":l yourFileName", ":main"] and press ENTER. The file will execute. Pressing F6+ENTER will load and run the file again. When you open Notepad++ next time, this will still be there. Whenever you want to work with a new file, you will have to change yourFileName of course.
Explanation: :cmd return " . . . " allows you to execute a string as multiple ghci commands, separated by \n. unlines takes a list of strings and joins them with \n. If you don't know about $, you'll learn it soon as it's part of basic Haskell.
If you don't have a main function in your file, then instead use :cmd return $ unlines [":l yourFileName"].
Note 1: For convenience, I used the Settings > ShortCutMapper > Plugin Commands to change Execute from F6 to F1.
Note 2: If you run multiple languages in this way (like maybe Lisp?), then you can replace the // This is a comment... line with // :cmd return $ unlines [":l yourFileName", ":main"] just so you have it for later when you switch back to Haskell.
Note 3: Instead of pasting cd "$(FULL_CURRENT_PATH)" stack ghci into the NppExec window, a much simpler way to do all this is to paste stack runghc "$(FULL_CURRENT_PATH)" and nothing else needs to be done. However I found the console takes a lot longer to load and run the file in that case, so the method above is what I use.

Haskell ghci command line. return value overwrite last prompt

I installed ghci on Max OSX.
But everytime, the return value overwrite my last ghci prompt.
See below.
Falseghci>null[1,2,3]
Luke_ghci>
"False" over write my last line ghci prompt. (should be Luke_ghci)
So weird. How to solve it?
Thanks.
Copied from the above-linked message:
Aha! That led me to find it: I had TERM set to 'ansi'. ghci +
Haskeline works find if TERM is set to any of rxvt, vt52, vt100, vt102
or xterm. Don't know what in terminfo Haskeline is relying on, but
'ansi' doesn't have it!

Resources