最好的写法是-> ..->[a] haskell 中的递归函数

发布于 2024-09-04 16:15:19 字数 555 浏览 3 评论 0原文

所以我一直遇到这个小问题,

func :: a -> b -> [a]  -- # or basically any a-> ...-> [a] where ... is any types  ->
func x y = func' [x] y -- '# as long as they are used to generate a list of [a] from x

func' :: [a] -> b -> [a] 
func' = undefined -- # situation dependant generates a list from
                  -- # each element and returns it as one long list

我应该保持这样吗?

我应该使用隐藏在何处的 func' 吗?

我应该只使用 [a] -> b-> [a] 版本并将传递[变量] 的责任留给被调用者?

我可能很需要组合这些函数,并且可能想打乱顺序,所以我倾向于选项 3。您觉得怎么样?

So I keep having this small problem where I have something like

func :: a -> b -> [a]  -- # or basically any a-> ...-> [a] where ... is any types  ->
func x y = func' [x] y -- '# as long as they are used to generate a list of [a] from x

func' :: [a] -> b -> [a] 
func' = undefined -- # situation dependant generates a list from
                  -- # each element and returns it as one long list

should I keep it like this?

should I use func' hidden by a where?

should I only use the [a] -> b -> [a] version and leave the responsibility of passing [variable] to the callee?

I might well need to compose these functions and might want to mess around with the order so I'm leaning towards option 3. What do you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走野 2024-09-11 16:15:19

看起来您正在尝试重新发明 concatMap:

concatMap :: (a -> [b]) -> [a] -> [b]
concatMap f = concat . map f

因此“map”位将输入列表的每个元素应用“f”。 “f”接受单个“a”并返回“[b]”。然后将这些单独的列表连接成一个列表。

It looks like you are trying to reinvent concatMap:

concatMap :: (a -> [b]) -> [a] -> [b]
concatMap f = concat . map f

So the "map" bit takes each element of the input list applies "f" to it. "f" takes a single "a" and returns a "[b]". These individual lists are then concatenated into a single list.

秋千易 2024-09-11 16:15:19

正如 Paul 指出的func' 可以替换为 concatMapfunc

func 本身让我想起了 unfoldr from Data.List

unfoldr :: (b -> Maybe (a, b)) -> b -> [a]

用于从生成a列表b.

顺便说一句,funcfunc' 对于此类函数来说是不幸的名称。

As Paul noted, func' can be replaced with concatMap and func.

And func itself reminds me of unfoldr from Data.List:

unfoldr :: (b -> Maybe (a, b)) -> b -> [a]

It is used to generate a list of a from b.

By the way, func and func' are unfortunate names for such functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文