gpt4 book ai didi

haskell - 如何使用重复变量以无点样式重写?

转载 作者:行者123 更新时间:2023-12-03 21:44:18 25 4
gpt4 key购买 nike

如何以无点样式重写以下表达式?

p x y = x*x + y

使用 lambda 演算我做了以下事情:
p = \x -> \y -> (+) ((*) x x) y
= \x -> (+) ((*) x x) -- here start my problem
= \x -> ((+) . ((*) x )) x
... ?

最佳答案

我问 lambdabot

<Iceland_jack> @pl p x y = x*x + y
<lambdabot> p = (+) . join (*)

join 来自 Control.Monad并且通常有这种类型
join :: Monad m => m (m a) -> m a

但使用 instance Monad ((->) x) (如果我们可以 left section types 这可以写成 (x ->) )我们得到以下类型/定义
join :: (x -> x -> a) -> (x -> a)
join f x = f x x

让我们让 GHCi 确认类型:
>> import Control.Monad
>> :set -XTypeApplications
>> :t join @((->) _)
join @((->) _) :: (x -> x -> a) -> x -> a

关于haskell - 如何使用重复变量以无点样式重写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130799/

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