Haskell error parse error on input `='

后端 未结 4 1887
迷失自我
迷失自我 2020-12-07 19:54

I\'m new to Haskell and after starting ghci I tried:

f x = 2 * x

and I obtained:

:1:4: pars         


        
相关标签:
4条回答
  • 2020-12-07 20:09

    In GHCi 7.x or below, you need a let to define things in it.

    Prelude> let f x = x * 2
    Prelude> f 4
    8
    

    Starting from GHC 8.0.1, top-level bindings are supported in GHCi, so OP's code will work without change.

    GHCi, version 8.0.1.20161213: http://www.haskell.org/ghc/  :? for help
    Prelude> f x = x * 2
    Prelude> f 4
    8
    
    0 讨论(0)
  • 2020-12-07 20:10

    A good rule of thumb for using ghci is that any code you enter should conform to do-block semantics; that is, you could assume syntactically that you're programming within the IO monad (if this is new terminology, don't worry! I'd highly recommend reading through this tutorial).

    This answer illustrates this point with an example, and may provide more working insight into the nature of IO and ghci.

    0 讨论(0)
  • 2020-12-07 20:17

    Starting in GHC 8.0.1 this would no longer generate an error.

    0 讨论(0)
  • 2020-12-07 20:28

    When you type into a Haskell source file,

    f x = 2 * x
    

    is correct.

    When you type directly into ghci, you need to type let at the start of the line:

    let f x = 2 * x
    
    0 讨论(0)
提交回复
热议问题