gpt4 book ai didi

haskell - 函数替代

转载 作者:行者123 更新时间:2023-12-01 15:32:29 27 4
gpt4 key购买 nike

我如何做一个函数(递归地)用参数 b(1) 替换列表中的参数 a(0)?例如:

    substitute 0 1 [1,0,3,0,4,0,0]
[1,1,3,1,4,1,1]

谢谢。

最佳答案

不需要递归!

substitute :: Eq a => a -> a -> [a] -> [a]
substitute old new = map subs where
subs x | x == old = new
| otherwise = x

如果这是家庭作业,您可以轻松地在 definition of map 中替换(递归的)。

关于haskell - 函数替代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219592/

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