Reputation: 19664
If I have a simple Haskell one-liner, what's the flag in ghc or ghci that could execute this from the command line?
I'm looking for something like:
stack ghci -e 'putStrLn "hello world"'
Similar to
$ R --quiet -e "cat('hello world')"
> cat('hello world')
hello world>
or
$ python -c "print('hello world')"
hello world
(This question is resolved with an excellent answer already, but just debugging that the flag seems like it /should/ work above...)
Weirdly couldn't get the seemingly supported ghci -e
working for me. Testing it is not just my machine, I also ran this on Ubuntu and had the same issue:
FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install --yes curl \
&& curl -sSL https://get.haskellstack.org/ | sh \
&& export HOME=/root/.local/bin:$HOME \
&& stack ghci -e 'putStrLn "hello world"'
Then
$ docker build .
Produced...
Stack has been installed to: /usr/local/bin/stack
WARNING: '/root/.local/bin' is not on your PATH.
For best results, please add it to the beginning of PATH in your profile.
Invalid option `-e'
Usage: stack ghci [TARGET/FILE] [--ghci-options OPTIONS] [--ghc-options OPTIONS]
[--flag PACKAGE:[-]FLAG] [--with-ghc GHC] [--[no-]load]
[--package ARG] [--main-is TARGET] [--load-local-deps]
[--[no-]package-hiding] [--only-main] [--trace] [--profile]
[--no-strip] [--[no-]test] [--[no-]bench] [--help]
Run ghci in the context of package(s) (experimental)
The command '/bin/sh -c apt-get update && apt-get install --yes curl && curl -sSL https://get.haskellstack.org/ | sh && export HOME=/root/.local/bin:$HOME && stack ghci -e 'putStrLn "hello world"'' returned a non-zero code: 1
Upvotes: 1
Views: 458
Reputation: 26161
If you check $ stack --help
at one point you see that
eval Evaluate some haskell code inline. Shortcut for
'stack exec ghc -- -e CODE'
So instead of doing like
$ stack exec ghc -- -e 'putStrLn "hello world"'
hello world
you may do like
$ stack eval 'putStrLn "hello world"'
hello world
Upvotes: 7
Reputation: 476554
In fact you already have this flag: it is just:
-e expr
Evaluate
expr
; see eval-mode for details
So you can write it like:
ghci -e 'putStrLn "hello world"'
In fact if you use stack ghci
, you just open ghci
with your application, but the -e
flag is not "stack-specific".
For example:
$ ghci -e 'putStrLn "hello world"'
hello world
Upvotes: 2