Haskell Compiling Problem using GHCi Windows - haskell

I have developed a haskell application which is tested with WinHugs interpreter working fine .. when i try to comiple the same application using WinGHCi it prompts a error
lexical error in string/character literal at character '\t'
I have used \t in IO Program to display text
Example :- putStr "\n \n \t \t Hello ! "
Any solutions ?

You may want to try this step-by-step guide:
Save your program in a file program.hs this file should contain a function main of the type IO () that is executed at the program's start.
Open a shell in the directory where this file is.
Type ghc -O3 --make program.hs to compile program.hs into an executable program.exe.
Try to run program.exe
If the error still occurs, please post some more code to aid debugging.

Related

Does Haskell's ghci support running a script and then quit, like python or perl?

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.

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

Give example of actual Parenthesis Hell program

Some of you may be familiar with Parenthesis Hell (code here). Well, nice way to waste some time between evening phone calls, right?
I successfully used ghc to compile these five files. The interpreter seems to work if I run
$ ./ph
But since I don't really understand Lisp/cdr/car stuff very well, I quickly ran into trouble trying to do a functioning program. I thought at least some of them were supposed to print something to standard output...
$ ./ph () ()
-bash: syntax error near unexpected token `)'
$ ./ph () (3)
$ ./ph () (())
$
$ ./ph (()())
-bash: syntax error near unexpected token `('
$ ./ph "()()" # suggestion in comment
ph: ()(): openFile: does not exist (No such file or directory)
$ ./ph # actual behavior with returns
()
()
ph: Prelude.read: no parse
Needless to say, any attempt at using the Hello World in the "doc" led to lots of "syntax error near unexpected token" messages. Any ideas? Or does everything evaluate to nil and so this is just an elaborate Lisp joke, except with two actual compilers? Thanks!
(Note: Lisp aficionados will be doing a service by teaching more people about how to interpret this parenthesis stuff, which I have to admit will be useful for me anyway in a completely different context.)
Edit 2: #dfeuer at least has a resolution which worked, though I do wonder whether it is possible to use interactively from the shell.
$ cat hello
(()()(()()(()()()()((()()(()(()((()((()()()((()((()()()((()((((()()(()()()()()()(((()(((()((()((((()(((()()(()()((()((()()()((()()(()()()()(()()()()(()()()()(()(())))))))))))))))))))))))))))))))))))))))))))))))))
$ ./ph hello
Hello world!
Bizarre and yet awesome.
I don't think it expects code in its arguments. It looks to me like it looks for filenames (and a command line option named -v).
It also seems like it only accepts one parenthesized expression per program. So something like () (()) would be invalid but (() (())) would be okay (or at least well-formed). If you wanted to use this from the command line, you could use echo since it also reads from stdin:
echo "(()()(()()(()()()()((()()(()(()((()((()()()((()((()()()((()((((()()(()()()()()()(((()(((()((()((((()(((()()(()()((()((()()()((()()(()()()()(()()()()(()()()()(()(())))))))))))))))))))))))))))))))))))))))))))))))))" | ./Test

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.

Redirecting Haskell GHCi output to text file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Outputting Haskell GHCi command results to a txt file
I am new to Haskell and I am trying to redirect test cases output results to a text file. The way it is set up now, is a AddAllTestCases.hs contains all the test cases I need to run in order to test a function I created. I run the test cases on GHCi by loading AddAllTestCases.hs and then simply typing main and hitting enter. That causes test case output results to print inside the GHCi perfectly.
Because there hundreds of test cases, I need to redirect output results to text file.
Attempt #1:
writeFile "myoutput.txt" $ show $ main
I get the following error:
No instance for (Show(IO())) arising from a use of show
Attempt #2 in CMD (trying to create an executable, then outputting executable results to text file):
ghc --make AddAllTests.hs -o testResults.exe
Which gives me the following error:
Warning: output was redirected with -o, but no output will be generated because there is no Min module
This is weird because when I am using GHCi (attempt #1) and I type in main it executes everything perfectly, which I would assume, implies that there is a main module?
I greatly appreciate any help with redirecting test case results to a text file.
Many thanks in advance!
You need a Main module (and a main action) to produce an executable. You can rename your module to Main, or you can specify the module to be considered Main on the command line,
ghc --make -main-is AddAllTests AddAllTests.hs -o testResults.exe
to produce an executable without a module named Main.
A method without compiling would be
ghc AddAllTests.hs -e "main" > testResults.txt
Another method would be to have a file in which you just list all test cases,
3 + 2 :: Rational
reverse "foobar"
:q
and run ghci with redirected in- and output
ghci < testCases > testResults.txt

Resources