代码之家  ›  专栏  ›  技术社区  ›  Mittenchops

用于执行表达式的Haskell堆栈命令行标志

  •  1
  • Mittenchops  · 技术社区  · 7 年前

    如果我有一个简单的Haskell单行程序,那么ghc或ghci中可以从命令行执行此操作的标志是什么?

    stack ghci -e 'putStrLn "hello world"'
    

    类似

    $ R --quiet -e "cat('hello world')"
    > cat('hello world')
    hello world> 
    

    $ python -c "print('hello world')"
    hello world
    

    为“ghci-e”调试进行编辑

    (这个问题已经用一个很好的答案解决了,但只是调试一下,上面的标志似乎是/应该/工作的…)

    ghci -e 为我工作。测试它不仅仅是我的机器,我也在Ubuntu上运行了它,并且遇到了同样的问题:

    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"'
    

    $ docker build .
    

    产生。。。

    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 回复  |  直到 7 年前
        1
  •  7
  •   Redu    7 年前

    如果你查一下 $ stack --help

    eval                     Evaluate some haskell code inline. Shortcut for
                             'stack exec ghc -- -e CODE'
    

    因此,与其像这样做

    $ stack exec ghc -- -e 'putStrLn "hello world"'
    hello world
    

    $ stack eval 'putStrLn "hello world"'
    hello world
    
        2
  •  2
  •   willeM_ Van Onsem    7 年前

    事实上你已经有了这个 flag :它只是:

    -e expr
    

    expr ; 有关详细信息,请参见评估模式

    ghci -e 'putStrLn "hello world"'

    事实上,如果你使用 stack ghci ,你刚开门 ghci -e 标志不是“特定于堆栈”。

    $ ghci -e 'putStrLn "hello world"'
    hello world
    
    推荐文章