简体   繁体   English

Haskell中的“ in”关键字有什么作用?

[英]What does the “in” keyword do in Haskell?

I am having trouble understanding the full scope of the 'in' keyword in haskell. 我在理解haskell中'in'关键字的全部范围时遇到了麻烦。 It was my understanding that you could use it to pass arguments to a function. 据我了解,您可以使用它来将参数传递给函数。 However, I am having trouble understanding how the applies to a function similar to that shown below: 但是,我在理解怎样将其应用于类似于以下所示功能的过程时遇到了麻烦:

foo xs a =
   case xs of
      [] -> (a,[])
      y:ys ->
         let (n,ns)=foo ys a in
         if y>0 then (1+n,y:ns)
         else (n,ns)

How does 'in' apply to the equation if it is supplying parameters that foo does not take? 如果提供了foo不带的参数,则“输入”如何应用于方程式?

in goes along with let to name one or more local expressions in a pure function. in与随之而来let来命名一个纯函数的一个或多个局部表达式。

So, to use a simpler example, 因此,举一个简单的例子,

foo = 
  let greeting = "hello" in 
    print (greeting ++ " world")

would print "hello world". 将打印“ hello world”。

But you could also use lots of let s: 但是您也可以使用很多let

foo = 
  let greeting = "hello"
      greetee  = "world" in
    print (greeting ++ " " ++ greetee)

And you just need one in before you use the names you just defined. in使用刚定义的名称之前,您只需要一个即可。

And these names definitely don't have to be used as parameters to a function -- they can be just about anything, even functions themselves! 而这些名字绝对不必作为参数传递给函数-他们可以是任何东西,甚至是函数本身!

foo = 
  let f = print in
    f "hello world"

In your example, the let expression is defining two names, n and ns , which are used in the body of the function. 在您的示例中, let表达式定义了两个名称nns ,它们在函数主体中使用。

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

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