1

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

Edit for 'ghci -e' debug

(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
2
  • 3
    stack eval 'putStrLn "hello world"' Commented Nov 25, 2018 at 19:02
  • @Redu, this works, can you post as an answer I can accept? Commented Nov 25, 2018 at 19:06

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

6 Comments

Hmm, I get: $ stack ghci -e 'putStrLn "hello world"' Invalid option -e' ` And don't otherwise have ghc installed outside of stack, if possible to keep it that way: $ ghc -e 'putStrLn "hello world"' Command 'ghc' not found, but can be installed with: sudo apt install ghc
@Mittenchops: what version of ghci do you use? This is already supported since 7.6 (or even eariler) iirc.
$ stack ghci Configuring GHCi with the following packages: GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help
@Mittenchops: in 8.4.3, this flag is also supported: downloads.haskell.org/~ghc/8.4.3/docs/html/users_guide/…, strange
@Mittenchops: well I work with Ubuntu as well, so this makes it even more strange :S
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.