gpt4 book ai didi

haskell - 如何在 Haskell 中编写自应用函数?

转载 作者:行者123 更新时间:2023-12-02 04:06:09 24 4
gpt4 key购买 nike

我尝试了以下代码,但它会生成类型错误。

sa f = f f
• Occurs check: cannot construct the infinite type: t ~ t -> t1
• In the first argument of ‘f’, namely ‘f’
In the expression: f f
In an equation for ‘sa’: sa f = f f
• Relevant bindings include
f :: t -> t1
(bound at fp-through-lambda-calculus-michaelson.hs:9:4)
sa :: (t -> t1) -> t1
(bound at fp-through-lambda-calculus-michaelson.hs:9:1)

最佳答案

使用新类型来构造无限类型。

newtype Eventually a = NotYet (Eventually a -> a)

sa :: Eventually a -> a
sa eventually@(NotYet f) = f eventually

在 GHC 中,最终f 将是内存中的同一个对象。

关于haskell - 如何在 Haskell 中编写自应用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42896907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com