gpt4 book ai didi

haskell - 如何将此 let/where 函数转换为 Haskell 中的 Lambda

转载 作者:行者123 更新时间:2023-12-02 13:28:58 25 4
gpt4 key购买 nike

如何将这个 let/where 函数转换为 Haskell 中的 lambda?

Let语句形式:

calc x =    let ad5 x = x + 5
sqr x = x * x
dbl x = x * 2
in
ad5 . sqr . dbl $ x

申报表在哪里:

calc x = ad5 . sqr . dbl $ x
where
ad5 x = x + 5
sqr x = x * x
dbl x = x * 2

Lambda 形式?可能类似于 Get Prog 中的这个示例,其中首先声明变量,然后在底部定义:

sumSqrOrSqrSum4 x y =   (\sumSqr sqrSum ->
if sumSqr > sqrSum
then sumSqr
else sqrSum) (x^2 + y^2) ((x + y)^2)

最佳答案

这个想法是这个let表达式:

let x = y in z

与这个 lambda 完全相同:

(\x -> z) y

其中 y 被用作参数,因此绑定(bind)到 x

就您而言,这将导致:

calc x = (\ad5 sqr dbl -> (ad5 . sqr . dbl $ x))
(\x -> x + 5)
(\x -> x * x)
(\x -> x * 2)

当然,除了这个练习之外,很少有人会真正这样写:)

关于haskell - 如何将此 let/where 函数转换为 Haskell 中的 Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54477343/

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