简体   繁体   English

生成保护功能时在 Haskell 中收到“变量不在范围内”错误

[英]Recieved "Variable not in Scope" error in Haskell when making guard function

I have this following code which has a guard function and prints out certain prices for a child, adult, and a senior:我有以下代码,它具有保护功能并打印出儿童、成人和老年人的某些价格:

tickets::String -> Integer -> Float
tickets x y
    |x == "Child" = 7.5 * fromIntegral y
    |x == "Adult" = 12.5 * fromIntegral y
    |otherwise = 8.0 * fromIntegral y
    
    
main = do
print& tickets "Child" 5

However when I try to run it, I get this error:但是,当我尝试运行它时,出现此错误:

main.hs:9:6: error:
Variable not in scope: (&) :: (a0 -> IO ()) -> Float -> t

The Float part was supposed to have the function print out the total price of 5 child tickets according to my main function, and I want to know how to avoid this error while learning Haskell. Float部分应该是根据我的main函数打印出5张儿童票总价的函数,我想知道在学习Haskell时如何避免这个错误。 It would be appreciated if someone would help me find out how to fix this error!如果有人能帮我找出如何解决这个错误,我将不胜感激!

Haskell can be challenging to learn at first. Haskell 刚开始学习可能具有挑战性。 Thankfully, you have some good tools at your disposal (stack, ghci, etc).幸运的是,您有一些很好的工具可供使用(stack、ghci 等)。

First, note the error you're getting.首先,请注意您遇到的错误。 It's saying that the variable (&) is not in scope.这是说变量(&)不在范围内。 That should be your first hint something's up/awry.这应该是你的第一个提示,有些事情出了问题/出了问题。

main.hs:9:6: error:
Variable not in scope: (&) :: (a0 -> IO ()) -> Float -> t

The above error message means that the compiler doesn't understand some variable (&) because it's not "in scope."上面的错误消息意味着编译器不理解某个变量(&)因为它不在“范围内”。 This means that, for whatever reason, the compiler can't "see" any definitions for this variable––either it isn't defined at all or the module it is defined in hasn't been imported.这意味着,无论出于何种原因,编译器都无法“看到”此变量的任何定义——要么根本没有定义它,要么没有导入定义它的模块。

The other thing you can do is inspect the types of the code you have now, compare these types to those the types you want to produce, and then fill in the gaps.您可以做的另一件事是检查您现在拥有的代码类型,将这些类型与您想要生成的类型进行比较,然后填补空白。

For example, you can start ghci with stack ghci in your project root.例如,您可以在项目根目录中使用stack ghci启动 ghci。 Then you can inspect the type of print :然后您可以检查print类型:

> :t print
print :: Show a => a -> IO ()

This says print takes some type a that implements Show .这表示print需要实现Show某种类型a If we then inspect tickets we see:如果我们然后检查tickets我们会看到:

:t tickets
tickets :: String -> Integer -> Float

Then as a quick "sanity check" we could check that Float's are showable:然后作为一个快速的“健全性检查”,我们可以检查 Float 是否可显示:

> show ((7.5)::Float)
"7.5"

Now we have a good idea about what main should look like:现在我们对main应该是什么样子有了一个好主意:

main :: IO ()
main = print (tickets "Child" 5)

As other people have pointed out, $ can used here ( $ can help reduce the number of parenthesis in your code):正如其他人所指出的, $可以在这里使用( $可以帮助减少代码中括号的数量):

main :: IO ()
main = print $ tickets "Child" 5

And there you have it!你有它! Hopefully this helps to demystify Haskell a bit :)希望这有助于揭开 Haskell 的神秘面纱:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM