GHCi的Haskell初学者错误
我正在Mac上试用Haskell,令我惊讶的是,当我定义一个函数时,出现了一个错误:GHCi的Haskell初学者错误,haskell,Haskell,我正在Mac上试用Haskell,令我惊讶的是,当我定义一个函数时,出现了一个错误: Prelude System.IO> :set prompt "ghci> " ghci> addMe :: Int -> Int -> Int <interactive>:11:1: error: Variable not in scope: addMe :: Int -> Int -> Int ghci> Prelude System.
Prelude System.IO> :set prompt "ghci> "
ghci> addMe :: Int -> Int -> Int
<interactive>:11:1: error:
Variable not in scope: addMe :: Int -> Int -> Int
ghci>
Prelude System.IO>:设置提示“ghci>”
ghci>addMe::Int->Int->Int
:11:1:错误:
变量不在范围内:addMe::Int->Int->Int
ghci>
如何在ghci上定义函数?如果要在ghci中的定义中添加类型签名,可以使用多行输入,如中所述,通过
:set+m
或:{…:}
,或使用分号来指定它,如中所述:
mulme x y = [x*y| x /= 0, y/= 0]; mulme :: Int -> Int -> [Int]
使用多行输入或显式分号,如我所示,我发现将所有定义放在一个文件中更容易,然后是代码>:加载< /代码>到GHCI。它将直接在解释器中键入的直接反馈经验与适当源文件的结构和备份功能相结合。请注意,我们通常使用
Maybe
,而不是始终最多包含一个元素的列表。通过这样做,上面定义的右侧可能会变成,如果(x/=0&&y/=0),那么就只有(x*y)了,其他什么都没有。