gpt4 book ai didi

haskell - 无点形式与风格

转载 作者:行者123 更新时间:2023-12-04 15:04:18 26 4
gpt4 key购买 nike

你能转换吗

-- tupleUnfold :: forall a. ((forall b. a -> b)) -> a -> ((b))
tupleUnfold :: Int -> ExpQ
tupleUnfold n = do
xs <- forM [1 .. n] (const . newName $ "x")
y <- newName "y"
let y' = varE y
g (ps', es') x = (varP x : ps', appE (varE x) y' : es')
(ps, es) = foldl' g ([], []) xs
lamE [tupP ps, varP y] (tupE es)

在保持清晰的同时保持无点风格(我知道程序“无点”,但更不想混淆代码)?

无论哪种方式,可以进行哪些更改来改进函数的样式,或者以其他方式使其意图更清晰?该功能旨在如下使用。
$(tupleUnfold 3) ((+ 1), (+ 2), (+ 3)) 2
-- (3, 4, 5)

使用哪些更好的命名约定(参见 ps、ps'、es 和 es' 变量)?

最佳答案

这就是我得到的。需求Control.Arrow (&&&)Control.Applicative (<$>) .

tupleUnfold :: Int -> ExpQ
tupleUnfold n = do
y <- newName "y"
(ps,es) <- unzip . map (varP &&& (`appE` varE y) . varE)
<$> replicateM n (newName "x")
lamE [tupP ps, varP y] (tupE es)

在不让它完全无法理解的情况下,不能再细化它了。

编辑 虽然不是免费的,但这是我能做到的最清楚的。需求 Data.Function (on)
tupleUnfold :: Int -> ExpQ
tupleUnfold n = do
y <- newName "y"
xs <- replicateM n (newName "x")
let exps = tupE $ zipWith appVars xs (repeat y)
pats = tupP $ map varP xs
lamE [pats, varP y] exps
where
appVars = appE `on` varE

关于haskell - 无点形式与风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4235741/

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