Can Haskell be used to write shell scripts? - linux

Is it possible to write shell scripts in Haskell and if so, how do you do it? Just changing the interpreter like so?
#!/bin/ghci

Using ghci will just load the module in GHCi. To run it as a script, use runhaskell or runghc:
#!/usr/bin/env runhaskell
main = putStrLn "Hello World!"

Well check this presentation : Practical Haskell: scripting with types

It should work if you change the interpreter to this:
#!/usr/bin/runhaskell

As of October 2016, there is a better answer to this question: use The Haskell Tool Stack script interpreter. stack-based Haskell scripts are portable because they download (and cache) the correct version of ghc and all of their package dependencies.
#!/usr/bin/env stack
-- stack --resolver lts-3.2 --install-ghc runghc
main = putStrLn "Hello World!"
See also the tutorial How to Script with Stack.

Another way to write shell scripts using Haskell is to generate the scripts, such as with bashkell. This is useful if you might want to run on systems that do not have haskell installed.

Related

Compile stack script instead of running it

The build tool stack has the feature to treat a usually compiled haskell source file as a script. (https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter)
Is it also possible to create a compiled executable the same way?
I searched the help section of stack and stack script, but could not find an options that make this possible.
script.hs:
#!/usr/bin/env stack
{-
stack script
--resolver lts-13.14
--package turtle
-}
main = print "hello"
So if given it the right permissions, this file can be executed. I guess behind the scenes stack compiles the file and then just runs it. And I'd like to just get the compiled intermediate binary.
No, it's not compiled. It's run through runhaskell which is a Haskell interpreter. If you want to compile it... do that. Instead of running the script,
take the --resolver and --package options from the script comment and pass them like this
stack ghc --resolver lts-13.14 --package turtle test.hs
The script command takes both --compile and --optimize as flags, which will instruct Stack to first compile to an executable (optionally with -O2 optimization level) before running.

Compiling simple programs on Haskell on Windows using the command line

This is a follow-up to this question, which is about whether it is possible to compile simple programs on Haskell in Windows, without recourse to Cygwin: Compiling Haskell programs in Windows: is it possible without downloading something such as Cygwin?
For background, I asked this question, since if there were some other way of compiling the program it would be very useful to know, since I am on a university computer and cannot download things like Cygwin without permission. (and even with permission it might not be possible, depending on what Cygwin requires)
Someone responded to my question, suggesting I open the command line and put ghc --make helloworld and hit Enter. However, when I put in ghc --make helloworld and hit Enter this comes up:
ghc: unrecognised flag: --
did you mean one of:
-D
-F
-H
Usage: For basic infomration, try the '--help' option
The person answering the question suggested I made another question, asking why I received the above message. How can I deal with this problem?
Yes, it is possible to use Windows to compile Haskell programs. In fact, I use Windows for all my Haskell programming! To compile a Haskell program, use ghc --make <program>; for instance, here it would be ghc --make helloworld.hs. Note that there is no space between -- and make; including this space gives the error you describe. After running this command, an executable helloworld.exe file is produced.

Running a haskell program in ghci

I am new to Haskell and trying to learn it from "Learn you a Haskell." I have run into a problem that I cannot find an answer to anywhere. I have a simple program that I want to run, but nothing will I do will make it run. What the book is telling me to do doesn't work. I can compile the program and run individual functions, but I can't get main to run unless I call that particular function. That seemed fine to me until I tried to pass a text file into it and it doesn't work.
So what do I do to run the program after typing :load program.hs?
I have tried...
$ ./program
--make program
--make program.exe
and about a thousand variations of these things. What the hell do I do to get my program running so that I can pass it a text file?
Picture of results in GHCi
cmd "Assembler failure"
It looks like you got confused between ghci and the command line. You can only type Haskell code in ghci. The command ./capslocker < haiku.txt is meant to be run from the command line and will run your compiled program capslocker. The $ sign is the command prompt in Linux and you're not meant to type that in. The book suggests using
$ ghc --make capslocker
beforehand to compile the code. It doesn't actually use ghci in this section. If you're on Windows then some of the commands may not work, since it assumes you are using Linux (it explains this earlier in the "input and output" section and suggests cygwin as an alternative).
Haskell can be compiled or interpreted. To use a python-like interpreter do runhaskell and you can use the same parameters as you would compile it.
More information here:
What's the difference between runghc and runhaskell?

How to load example in Djinn/UU/Examples/Equality.hs?

After cabal install Djinn which is using QuickCheck, the executable file is in ./.cabal/bin/djinn.
then I copy the executable to the directory Downloads/Djinn/UU/Examples/
want to run example Equality.hs then djinn Equality.hs, can not parse command
then ./djinn then :load full path/UU/Examples/Equality.hs
return cannot parse command
As the comment says, this doesn't make much sense. Djinn doesn't work on Haskell source files as far as I know. Also Djinn doesn't appear to use quickcheck.

How to do i18n and create Windows Installer of Haskell programs?

I'm considering using Haskell to develop for a little commercial project. The program must be internationalized (to Simplified Chinese, to be specific), and my customer requests that it should be delivered in a one-click Windows Installer form. So basically these are the two problems I'm facing now:
I18n of Haskell programs: the method described in Internationalization of Haskell programs did work (partially) if I change the command of executing the program from LOCALE=zh_CN.UTF-8 ./Main to LANG=zh_CN.UTF-8 ./Main (I'm working on Ubuntu 10.10), however, the Chinese output is garbled, and I've no idea why is that.
Distribution on Windows: I'm used to work under Linux and build & package my Haskell programs using Cabal, but what is the most natural way to create a one-click Windows Installer from a cabalized Haskell package? Is the package bamse the right way to go?
------ Details for the first problem ------
What I did was:
$ hgettext -k __ -o messages.pot Main.hs
$ msginit --input=messages.pot --locale=zh_CN.UTF-8
(Edit the zh_CN.po file, adding Chinese translation)
$ mkdir -p zh_CN/LC_MESSAGES
$ msgfmt --output-file=zh_CN/LC_MESSAGES/hello.mo zh_CN.po
$ ghc --make Main.hs
$ LANG=zh_CN.UTF-8 ./Main
And the output was like:
This indicates gettext is actually working, but for some reason the generated zh_CN.mo file is broken (my guess). I'm pretty sure my zh_CN.po file is encoded in UTF-8. Plus, aside from using System.IO.putStrLn, I also tried System.IO.UTF8.putStrLn to output the string, which didn't work either.
For the first question, the solution is to add decodeString from Codec.Binary.UTF8.String to the gettext function __, like this:
__ :: String -> String
__ = decodeString . unsafePerformIO . getText
And then everything works just fine.
for the first question, is it possible that you didn't install correct font or the font is not set to be used in the terminal.

Resources