My haskell program produces .tex output using my own module.
I can't see my way around having to run 'pdflatex' everytime I want to see a result. I can hardly believe this cannot be done in a more direct manner;
Is there a way to have my code compile the .tex-file?
Is the System.Process package what you are looking for? You can use the function
system :: String -> IO ExitCode
to perform system calls.
$ touch tempfile.txt
$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
>> import System.Process
>> system "ls"
tempfile.txt
ExitSuccess
Not particularly idiomatic, but it gets the job done.
Related
Is there any way I can run GHCi on command line like a regular command in shell?
For example: :browse in GHCi - list all the function for specific module.
but I want to run it on shell, e.g.: ghci --browse "MyModule"
which lists all the functions for the module
I know hoogle can run it on shell, e.g: hoogle Monad
The easiest way is to pipe in the commands via standard input. In Bash this can be done nicely with a “here-string”:
$ ghci <<< ':t reverse'
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/sagemuej/.ghci
Loaded GHCi configuration from /home/sagemuej/.ghc/ghci.conf
Prelude> reverse :: [a] -> [a]
Prelude> Leaving GHCi.
Use verbosity 0 to avoid all the greeting stuff:
$ ghci -v0 <<< ':t reverse'
reverse :: [a] -> [a]
I am on a mac OS X EL Captain and try to figure out how to signal EOF , however ctrl + D doesn't work. I have confirmed with the terminal using stty all that eof = ^D.
With the following code in Haskell
Module Input where
import Data.List
import Data.Char
import System.IO (isEOF)
main =
interact (concat . sort . lines)
The expression will never give me any stdout - I have tried ctrl+d , but nothing happens. Why is this?
I run this program in GHCI
(Note: Question about running the program in GHCI.)
According to this TRAC issue from 9 years ago it is not considered a bug.
You can hSetBuffer stdin LineBuffering and Control-D will be recognized, but GHCI will also raise a "handle is closed" error when it comes back to the REPL prompt and the session will terminate:
$ ghci ./control-d.hs
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Input ( control-d.hs, interpreted )
Ok, modules loaded: Input.
*Input> import System.IO
*Input System.IO> hSetBuffering stdin LineBuffering
*Input System.IO> :main
jhskdfjhdf
jhskdfjhdf<stdin>: hGetBuffering: illegal operation (handle is closed)
$
Python or Perl supports:
python xxx.py
perl xxx.pl
powershell xxx.ps1
Execute the script and quit with an exit code. When I tried GHCi (as ghci xxx.hs) it seems to load the xxx.hs file and enter interactive mode, without quiting.
Does the GHC interpreter support such operations?
Does this "interpreting" require to have a main function like the GHC compiler does?
Use runghc to get the same behavior. You will indeed need a main :: IO () function still.
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.
How could I execute a system command such as cp somefile somedestination in Haskell?
Something like an os.Exec.
The Haskell 98 standard provides:
System.system :: String
-> IO GHC.IO.Exception.ExitCode
which executes a command.
The new System.Process library is more useful though, allowing for portable input/output redirection and so forth.
import System.Process
main = callCommand "cp somefile somedestination"
just works, but you may prefer other functions from the System.Process module.
I'm not a haskell buff, but this may be what you're looking for
If you do this sort of thing a lot then it's worth having a look at http://hackage.haskell.org/package/HSH.