如何使用 ghci 在 lambda 中写入数字

发布于 2024-08-09 03:42:41 字数 238 浏览 3 评论 0原文

我是 Haskell 新手,使用 Ghci。

我有一个名为三的函数,我想将其写为

let three =  \x->(\y->(x(x(x y)))) 

“OK”,这可以工作,但是当我尝试时

three (2+) 4

它不起作用。相反,我收到一些“无法构造无限类型”错误。

请帮我。

I am new to Haskell, using Ghci.

I have a function, called three, that I want to write as

let three =  \x->(\y->(x(x(x y)))) 

OK, this works, but when I try

three (2+) 4

It does not work. Instead, I get some "cannot construct infinite type" error.

Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我为君王 2024-08-16 03:42:41
ghci> let three = \x->(\y->(x(x(x y))))
ghci> three (2+) 4
10
ghci> three return "deconstructivist"

<interactive>:1:6:
    Occurs check: cannot construct the infinite type: t = m t
      Expected type: t
      Inferred type: m t
    In the first argument of 'three', namely 'return'
    In the expression: three return "deconstructivist"
ghci> :t three
three :: (t -> t) -> t -> t
ghci> :t return
return :: (Monad m) => a -> m a
  • 您提供的三 (2+) 4 示例有效!最好检查您提供的示例是否确实重现了您的问题。
  • 至于另一个示例,例如上面的 return 示例,问题是 return 结果的类型与给定的类型不同。如果类型相同,它将是无限的(并且类型为 * -> * -> * -> ...),Haskell 不支持这种情况。
ghci> let three = \x->(\y->(x(x(x y))))
ghci> three (2+) 4
10
ghci> three return "deconstructivist"

<interactive>:1:6:
    Occurs check: cannot construct the infinite type: t = m t
      Expected type: t
      Inferred type: m t
    In the first argument of 'three', namely 'return'
    In the expression: three return "deconstructivist"
ghci> :t three
three :: (t -> t) -> t -> t
ghci> :t return
return :: (Monad m) => a -> m a
  • The example you supplied of three (2+) 4, works! Better check that the examples you provide actually reproduce your problem.
  • As for with a different example, like the one above with return, the thing is that return results in a different type than the one given. If the type was the same, it would be infinite (and of kind * -> * -> * -> ...), which Haskell does not support.
著墨染雨君画夕 2024-08-16 03:42:41

你给出的例子确实有效。让我们解释一下原因:

three f = f . f . f
-- so...
three :: (a -> a) -> a -> a

该函数需要具有类型 a -> a 因为它将接收它自己的参数,这需要一个类型。 (2+) 的类型为 Num a =>;一个-> a,所以三 (2+) 4 就可以正常工作。

但是,当您传递 return 等类型为 Monad m => 的函数时,一个-> m a,它返回不同的类型,它与我们设定的(a -> a)要求不匹配。这就是你的函数将在何时何地失败的地方。

当你这样做时,尝试创建一个类似 doTimes 的函数,其类型为 Integer ->; (a->a)->一个-> a 执行给定的函数给定的次数 - 这是创建此函数后的一个很好的下一步。

The example you give does work. Let's explain why:

three f = f . f . f
-- so...
three :: (a -> a) -> a -> a

The function needs to have type a -> a because it will receive it's own argument, which requires a type. (2+) has type Num a => a -> a, so three (2+) 4 will work just fine.

However, when you pass a function like return of type Monad m => a -> m a, which returns a different type, it will not match the (a -> a) requirement we set out. This is where and when your function will fail.

While you're at it, try making a function like doTimes with type Integer -> (a -> a) -> a -> a which does the given function the given number of times - it's a good next step after making this function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文