signal EOF of stdin when in mac OS X terminal with ctrl+D doesn't work - haskell

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)
$

Related

Why doesn't my Haskell cmd line program get arguments from Vim Bang?

Vim has the possibility to let you replace selected text with the output of an external program. I'd like to take advantage of this with programs that I'd write in Haskell. But it doesn’t get the selected text as args.
-- show-input.hs
module Main where
import System.Environment
main = do
input <- getArgs
putStr ("Input was: " ++ (show input))
When I run it from the command line (NixOS GNU/Linux, BASH), I get the expected behavior:
$ ./show-input test
Input was: ["test"]
When I select some text in Vim and invoke :'<,'>!~/show-input, I get this :
Input was: []
There is something weird here, but I can't tell if it is from the way Vim passes arguments or from the way Haskell gets them. I have tried with both console Vim and graphical gVim (8.0.1451), with the same result.
NB: I can successfully use Vim Bang! with other external programs, such as grep. It works great.
---
Correct version after chepner's answer
So, for anyone interested, just replace getArgs with getContents and you get your input all in a string (instead of a list of strings).
module Main where
import System.Environment
main = do
input <- getContents
putStr ("Input was: " ++ (show input))
The ! command sends the seleted text to the program via standard input, not as a command line argument. The command line equivalent would be somecommand | ./show-input.

Weird buffering with prompt in haskell [duplicate]

This question already has answers here:
Why doesn't Haskell sequence these IO actions properly?
(2 answers)
Closed 7 years ago.
I was playing around in Haskell and noticed something weird. I've defined a simple prompt function below.
-- file: test.hs
main :: IO ()
main = putStrLn . ("Hello, " ++) =<< (putStr "Name: " >> getLine)
When I runhaskell this, it works as expected, printing the prompt, waits for my input, then prints the greeting.
$ runhaskell test.hs
Name: kwarrtz
Hello, kwarrtz
When I compile it, however, things get weird. When I run it, it doesn't print the prompt, instead giving me a blank line and waiting for input. When I type my name and hit enter, it prints the prompt and the greeting, on the same line. In otherwords, the getLine happens before the putStr.
$ ghc test.hs
$ ./test
kwarrtz
Name: Hello, kwarrtz
Any thoughts on what's happening? I imagine it has something to do with the line buffering on my terminal, but I'm not sure how (that or I've just made some really ridiculous mistake in my code).
I'm running GHC 7.8.3 on Mac OS X El Capitan and using the default Terminal app.
Buffering.
hSetBuffering stdout NoBuffering
I think you can get all that from System.IO

IO monad and ordering [duplicate]

I have the following code:
main = do
putStr "Test input : "
content <- getLine
putStrLn content
When I run it (with runhaskell) or compile it (ghc 6.10.4) the result is like this:
asd
Test input : asd
Why is Test input : asd being printed after asd?
In the code sample on http://learnyouahaskell.com/, which uses putStr, the getLine's presented output is different than mine. When I use putStrLn the program works as expected (print, then prompt, and print).
Is it a bug in ghc, or it is the way that it should work?
This is because ghci disables buffering, while a program compiled with ghc has line buffering by default. You can see this by running this:
import System.IO
main = print =<< hGetBuffering stdout
In ghci you see NoBuffering while with runghc you get LineBuffering. Since the newline character doesn't print until after the user input, the prompt doesn't either.
Fix it by adding hFlush stdout after your prompt (or disable buffering with hSetBuffering stdout NoBuffering, but that’s probably bad).

Modifying Emacs Inferior Haskell processes to enable CPP processing

If we look at the source of The random package we have a file Random.hs. Because of CPP extensions one has to invoke ghci via the following command :
ghci -cpp Random.hs
Alternatively one can do :
ghci -cpp
and then from within ghci :
Prelude GOA> :load Random
[1 of 1] Compiling System.Random ( Random.hs, interpreted )
Ok, modules loaded: System.Random.
If I use Emacs Inferior Haskell mode (Emacs/Inferior Haskell processes) and I have
the source :
module Main where
import System.Random
gen = (random (mkStdGen 0)) :: (Bool,StdGen)
mymult :: Int -> Int
mymult x = 2 * x
main = do
print $ mymult 5
then upon typing the emacs command :
C-c C-l
which is inferior-haskell-load-file, ghci is opened in a subwindow in emacs. However if from within this window I type load Random.hs then I get the error message :
*Main GOA> :load Random.hs
Random.hs:1:2: lexical error at character 'i'
Failed, modules loaded: none.
How can I load Random.hs taking into account cpp extensions? Or alternatively how do I modify haskell-mode/inf-haskell.el such that ghci is invoked with the -cpp option upon typing C-c C-l, so that the command :load Random.hs can be executed without error?
The most reliable way is certainly to request CPP, along with other extensions, in the file header:
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module : System.Random
-- Copyright : (c) The University of Glasgow 2001
-- License : BSD-style (see the file LICENSE in the 'random' repository)
The random package only does this in the .cabal file.
The easiest way might be to simply turn CCP on permanently in all ghci sessions, by adding
:set -XCPP
to your ~/.ghci file.

latexpdf-esque functionality in haskell?

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.

Resources